目录
【REST风格】—— Representational State Transfer
【概述】
表现形式状态转换
【描述】
传统风格资源描述形式
REST风格描述形式
【优点】
按照REST风格访问资源时使用行为动作区分对资源进行了何种操作
根据REST风格对资源进行访问称为RESTful
【注意】
1、设置http请求动作
- @RequestMapping(value = "/users", method = RequestMethod.POST)
- @ResponseBody
- public String save(@RequestBody User user){
- System.out.println("user save..." + user);
- return "{'module':'user save'}";
- }
-
- @RequestMapping(value = "/users" ,method = RequestMethod.PUT)
- @ResponseBody
- public String update(@RequestBody User user){
- System.out.println("user update..."+user);
- return "{'module':'user update'}";
- }
2、设置请求参数(路径变量)
- @RequestMapping(value = "/users/{id}" ,method = RequestMethod.DELETE)
- @ResponseBody
- public String delete(@PathVariable Integer id){
- System.out.println("user delete..." + id);
- return "{'module':'user delete'}";
- }
【@RequestMapping】
例:
- @RequestMapping(value = "/users", method = RequestMethod.POST)
- @ResponseBody
- public String save(@RequestBody User user){
- System.out.println("user save..." + user);
- return "{'module':'user save'}";
- }
【@PathVariable】
例:
- @RequestMapping(value = "/users/{id}" ,method = RequestMethod.DELETE)
- @ResponseBody
- public String delete(@PathVariable Integer id){
- System.out.println("user delete..." + id);
- return "{'module':'user delete'}";
- }
【注意】
@RequestBody、@RequestParam、@PathVariable的区别
应用
【@RestController】
例:
- @RestController
- public class BookController {
- }
【@GetMapping、@PostMapping、@PutMapping、@DeleteMapping】
- @GetMapping("/{id}")
- public String getById(@PathVariable Integer id){
- System.out.println("book getById..."+id);
- return "{'module':'book getById'}";
- }
1.创建工程
2.SSM整合
3.功能模块
1、设置统一数据返回结果类
- public class Result {
- private Object data;
- private Integer code;
- private String msg;
- }
【注意】
2、设置统一数据返回结果编码
- public class Code {
- public static final Integer SAVE_OK = 20011;
- public static final Integer DELETE_OK = 20021;
- public static final Integer UPDATE_OK = 20031;
- public static final Integer GET_OK = 20041;
-
- public static final Integer SAVE_ERR = 20010;
- public static final Integer DELETE_ERR = 20020;
- public static final Integer UPDATE_ERR = 20030;
- public static final Integer GET_ERR = 20040;
- }
【注意】
Code类的常量设计也不是固定的,可以根据需要自行增减,例如将查询再进行细分为GET_OK,GET_ALL_OK,GET_PAGE_OK
3、根据情况设定合理的Result
- @RequestMapping("/books")
- public class BookController {
- @Autowired
- private BookService bookService;
- @GetMapping("/{id}")
- public Result getById(@PathVariable Integer id) {
- Book book = bookService.getById(id);
- Integer code = book != null ? Code.GET_OK : Code.GET_ERR;
- String msg = book != null ? "" : "数据查询失败,请重试!";
- return new Result(code,book,msg);
- }
- }
出现异常现象的常见位置与常见原因:
【注意】
集中的、统一的处理项目中出现的异常
- @RestControllerAdvice
- public class ProjectExceptionAdvice {
- @ExceptionHandler(Exception.class)
- public Result doException(Exception ex){
- return new Result(666,null,"异常");
- }
- }
【@RestControllerAdvice】
【注意】
此注解自带@ResponseBody注解与@Component注解,具备对应的功能
【@ExceptionHandler】
【注意】
此类方法可以根据处理的异常不同,制作多个方法分别处理对应的异常
业务异常(BusinessException)
系统异常(SystemException)
其他异常(Exception)
1、自定义项目系统级异常
- public class SystemException extends RuntimeException{
- private Integer code;
-
- public Integer getCode() {
- return code;
- }
-
- public void setCode(Integer code) {
- this.code = code;
- }
-
- public SystemException(Integer code) {
- this.code = code;
- }
-
- public SystemException(Integer code,String message) {
- super(message);
- this.code = code;
- }
-
- public SystemException(Integer code, String message, Throwable cause) {
- super(message, cause);
- this.code = code;
- }
-
- public SystemException(Integer code, Throwable cause) {
- super(cause);
- this.code = code;
- }
-
- public SystemException(Integer code, String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
- super(message, cause, enableSuppression, writableStackTrace);
- this.code = code;
- }
- }
2、自定义项目业务级异常
- public class BusinessException extends RuntimeException{
- private Integer code;
-
- public Integer getCode() {
- return code;
- }
-
- public void setCode(Integer code) {
- this.code = code;
- }
-
- public BusinessException(Integer code) {
- this.code = code;
- }
-
- public BusinessException(Integer code, String message) {
- super(message);
- this.code = code;
- }
-
- public BusinessException(Integer code, String message, Throwable cause) {
- super(message, cause);
- this.code = code;
- }
-
- public BusinessException(Integer code, Throwable cause) {
- super(cause);
- this.code = code;
- }
-
- public BusinessException(Integer code, String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
- super(message, cause, enableSuppression, writableStackTrace);
- this.code = code;
- }
- }
3、自定义异常编码
- public class Code {
- public static final Integer SYSTEM_ERR = 50001;
- public static final Integer SYSTEM_TIMEOUT_ERR = 50001;
- public static final Integer SYSTEM_UNKNOW_ERR=59999;
-
- public static final Integer BUSINESS_ERR = 60002;
- }
4、触发自定义异常
- public Book getById(Integer id) {
- if(id==1){
- throw new BusinessException(Code.BUSINESS_ERR,"请勿进行非法操作");
- }
- try {
- int i=1/0;
- }catch (Exception ex){
- throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服务器访问超时,请稍后再试");
- }
- return bookDao.getById(id);
- }
5、拦截并处理异常
- @RestControllerAdvice
- public class ProjectExceptionAdvice {
- @ExceptionHandler(SystemException.class)
- public Result doSystemException(SystemException ex) {
- return new Result(ex.getCode(), null, ex.getMessage());
- }
-
- @ExceptionHandler(BusinessException.class)
- public Result doBusinessException(BusinessException ex) {
- return new Result(ex.getCode(), null, ex.getMessage());
- }
-
- @ExceptionHandler(Exception.class)
- public Result doException(Exception ex) {
- return new Result(Code.SYSTEM_UNKNOW_ERR, null, "系统异常,请稍后再试");
- }
- }
【概述】
是一种动态拦截方法调用的机制,在SpringMVC中动态拦截控制器方法的执行
【作用】
1、声明拦截器的bean,并实现HandlerInterceptor接口(注意:扫描加载bean)
- @Component
- public class ProjectInterceptor implements HandlerInterceptor {
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
- System.out.println("preHandle");
- return true;
- }
-
- @Override
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
- System.out.println("postHandle");
- }
-
- @Override
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
- System.out.println("afterCompletion");
- }
- }
2、定义配置类,继承WebMvcConfigurationSupport,实现addInterceptor方法(注意:扫描加载配置)
- @Configuration
- public class SpringMvcSupport extends WebMvcConfigurationSupport {
-
- @Override
- protected void addInterceptors(InterceptorRegistry registry) {
- }
- }
-
-
3、添加拦截器并设定拦截的访问路径,路径可以通过可变参数设置多个
- @Configuration
- public class SpringMvcSupport extends WebMvcConfigurationSupport {
- @Autowired
- private ProjectInterceptor projectInterceptor;
-
- @Override
- protected void addInterceptors(InterceptorRegistry registry) {
- registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");
- }
- }
【可使用标准接口WebMvcConfigurer简化开发】(注意:侵入式较强)
- @Configuration
- @ComponentScan("com.itheima.controller")
- @EnableWebMvc
- public class SpringMvcConfig implements WebMvcConfigurer{
- @Autowired
- private ProjectInterceptor projectInterceptor;
-
- public void addInterceptors(InterceptorRegistry registry) {
- registry.addInterceptor(projectInterceptor).addPathPatterns("/books", "/books/*");
- }
- }

【前置处理】
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
- System.out.println("preHandle");
- return true;
- }
【参数】
【返回值】
返回值为false,被拦截的处理器将不执行
【后置处理】
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
- System.out.println("postHandle");
- }
【参数】
modelAndView:如果处理器执行完成具有返回结果,可以读取到对应数据与页面信息,并进行调整
【完成后处理】
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
- System.out.println("afterCompletion");
- }
【参数】
ex:如果处理器执行过程中出现异常对象,可以针对异常情况进行单独处理
【多拦截器执行顺序】

【注意】
与过滤器链执行类似