异常返回:
需要将原本的错误格式转换为JSON格式
异常注解:
@ControllerAdvice:需要表明一个处理异常的类
@ExceptionHandler:异常的处理器 ,接受一个参数
Error 错误
Exception 异常
CheckedException:编译时进行处理
RuntimeException:运行时异常
HttpException extends RuntimeException
APIException extends Exception
可处理的BUG:Checked
不可处理:Runtime
已知异常 未知异常
未知异常:对于前端开发者和用户 都是无意义的。服务端开发者代码逻辑有问题
@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR) 修改响应码需要动态改变code码的已知异常处理代码示例@ExceptionHandler(HttpException.class)public ResponseEntity handleHttpException(HttpServletRequest req,HttpException e){String requestUrl = req.getRequestURI();String method = req.getMethod();UnifyResponse message = new UnifyResponse(e.getCode(),"2212",method+" "+ req);//未使用@Response注解 所以需要自己写headersHttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);//动态设置HttpStatus//使用spring的静态方法 不用NEW一个对象HttpStatus httpStatus = HttpStatus.resolve(e.getHttpStatusCode());ResponseEntity<UnifyResponse> r = new ResponseEntity<>(message,headers,httpStatus);return r ;}
自定义配置类管理配置文件
异常的message文本 通常会放到单独的配置文件中,而单独的配置文件则需要一个单独的类来调用,这是spring提倡的,而在单独的类中可以使用MAP(键值对)
需要@Component注解将类注入到容器中
指定文件的路径并与类相关联:@PropertySource(value=”classPath:”)类路径下
读取前缀: @ConfigurationProperties(prefix=”前缀名”)
package com.lin.missyou.core.configuration;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;import java.util.HashMap;import java.util.Map;@Component@ConfigurationProperties(prefix="lin")@PropertySource(value = "classPath:config/exception-code.properties")public class ExceptionCodeConfiguration {private Map<Integer, String> codes = new HashMap<>();public Map<Integer, String> getCodes() {return codes;}public String getMessage(int code){String message = codes.get(code);return message;}}
根据目录结构自动生成路由前缀
在很多其他框架中,比如Python的Flask、node.js的KOA,Controller要想能够响应前端的请求都需要我们主动去注册到应用程序上。而Spring不需要我们自己去注册,由Spring通过扫描注解的方式去主动发现。
自定义RequestMappingInfo
Spring中的RequestMappingHandlerMapping专门来负责处理标注了@RequestMapping的控制器。创建一个类继承并覆盖其中的方法,从而实现对默认机制的修改。覆盖其中的getMappingForMethod方法,这个方法的返回值RequestMappingInfo就包含了请求的Url,修改RequestMappingInfo中的Url从而修改路由中的Url。
package com.lin.missyou.hack;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.servlet.mvc.method.RequestMappingInfo;import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;import java.lang.reflect.Method;public class AutoPrefixUrlMapping extends RequestMappingHandlerMapping {@Value("${missyou.api-package}")private String apiPackagePath ; //从配置文件中获取根包的路径@Overrideprotected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {RequestMappingInfo requestMappingInfo = super.getMappingForMethod(method, handlerType);if(null != requestMappingInfo){//获取url前缀String prefix = getPrefix(handlerType);//根据url前缀生成RequestMappingInfo并与原有的RequestMappingInfo合并RequestMappingInfo mappingInfo = RequestMappingInfo.paths(prefix).build().combine(requestMappingInfo);return mappingInfo;}return requestMappingInfo;}private String getPrefix(Class<?> handlerType){String packageName = handlerType.getPackage().getName(); //获取控制器所在包路径String dotPath = packageName.replaceAll(this.apiPackagePath,""); //将包路径中多于的部分截取掉return dotPath.replace(".","/"); //将包路径中的.替换成/}}
通过接口的形式发现类
创建一个配置类AutoPrefixConfiguration将AutoPrefixUrlMapping加入到容器。配置类AutoPrefixConfiguration实现接口WebMvcRegistrations并覆盖其中的getRequestMappingHandlerMapping方法
package com.lin.missyou.core.config;import com.lin.missyou.hack.AutoPrefixUrlMapping;import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;import org.springframework.stereotype.Component;import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;@Componentpublic class AutoPrefixConfiguration implements WebMvcRegistrations {@Overridepublic RequestMappingHandlerMapping getRequestMappingHandlerMapping() {return new AutoPrefixUrlMapping();}}
在配置文件中指定根包
missyou.api-package = com.lin.missyou.api
SprinBoot的 发现机制 有两种。一种是在控制器上标注特定注解,例如上一篇博文 SpringBoot全局异常处理中在GlobalExceptionAdvice 上标注@ControllerAdvice。另外一种是实现特定接口并覆盖其中的特定方法,例如上面的AutoPrefixConfiguration。
测试一下
访问结果,访问路径/v1/banner/test可以访问到该控制器
将访问路径改为/banner/test就访问不到了
将BannerController移动到sample文件夹下访问路径/v1/sample/banner/test可以访问到该控制器
