構建美觀且高效的 Spring Boot 腳手架
咱們先看看如何用IDE創建springboot項目,然后一路next就行了。
image.png
這就是剛創建好的項目,新鮮出爐,有啟動類、配置文件、測試啟動類。
image.png
版本管理
咱們的項目就這么輕松的創建成功了,是不是可以上手開發了,先別著急。先看一下版本之間的對應關系:https://spring.io/projects/spring-cloud
image.png
接下來我們來看看都有哪些核心類,我把代碼貼到下方。
image.png
全局異常處理
@RestControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(value = {MethodArgumentNotValidException.class})
public ResponseResult<String> handleValidException(MethodArgumentNotValidException ex, HttpServletResponse httpServletResponse) {
log.error("[GlobalExceptionHandler][handleValidException] 參數校驗exception", ex);
return wrapperBindingResult(ex.getBindingResult(), httpServletResponse);
}
private ResponseResult<String> wrapperBindingResult(BindingResult bindingResult, HttpServletResponse httpServletResponse) {
StringBuilder errorMsg = new StringBuilder();
for (ObjectError error : bindingResult.getAllErrors()) {
if (error instanceof FieldError) {
errorMsg.append(((FieldError) error).getField()).append(": ");
}
errorMsg.append(error.getDefaultMessage() == null ? "" : error.getDefaultMessage());
}
httpServletResponse.setStatus(HttpStatus.BAD_REQUEST.value());
return ResponseResult.failed(ResultCode.FAILED.getCode(),null);
}
日志處理
@Aspect
@Slf4j
@Component
public class WebLogAspect {
@Pointcut("@within(org.springframework.stereotype.Controller) || @within(org.springframework.web.bind.annotation.RestController)")
public void cutController() {
}
@Before("cutController()")
public void doBefore(JoinPoint point) {
//獲取攔截方法的參數
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String url = request.getRequestURL().toString();
List<Object> list = Lists.newArrayList();
for (Object object : point.getArgs()) {
if (object instanceof MultipartFile || object instanceof HttpServletRequest || object instanceof HttpServletResponse || object instanceof BindingResult) {
continue;
}
list.add(object);
}
log.info("請求 uri:[{}],params:[{}]", url, StringUtils.join(list, ","));
}
/**
* 返回通知:
* 1. 在目標方法正常結束之后執行
* 1. 在返回通知中補充請求日志信息,如返回時間,方法耗時,返回值,并且保存日志信息
*
* @param response
* @throws Throwable
*/
@AfterReturning(returning = "response", pointcut = "cutController()")
public void doAfterReturning(Object response) {
if (response != null) {
log.info("請求返回result:[{}]", JSONUtil.toJsonStr(response));
}
}
}
跨域類
@Configuration
public class GlobalCorsConfig {
/**
* 允許跨域調用的過濾器
*/
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
//允許所有域名進行跨域調用
config.setAllowedOrigins(Lists.newArrayList("*"));
//允許跨越發送cookie
config.setAllowCredentials(true);
//放行全部原始頭信息
config.addAllowedHeader("*");
//允許所有請求方法跨域調用
config.addAllowedMethod("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
@Configuration
@EnableOpenApi
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo()).enable(true)
.select()
//apis: 添加swagger接口提取范圍
.apis(RequestHandlerSelectors.basePackage("com.vines.controller"))
//.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("項目描述")
.description("基礎服務項目描述")
.contact(new Contact("作者", "作者URL", "作者Email"))
.version("1.0")
.build();
}
}
響應體
@Data
public class ResponseResult<T> {
private int code;
private String message;
private T data;
public static <T> ResponseResult<T> success(T data){
ResponseResult<T> respnotallow=new ResponseResult<>();
responseResult.setCode(ResultCode.SUCCESS.getCode());
responseResult.setMessage(ResultCode.SUCCESS.getMessage());
responseResult.setData(data);
return responseResult;
}
public static <T> ResponseResult<T> success(){
ResponseResult<T> respnotallow=new ResponseResult<>();
responseResult.setCode(ResultCode.SUCCESS.getCode());
responseResult.setMessage(ResultCode.SUCCESS.getMessage());
return responseResult;
}
public static <T> ResponseResult failed(int code,String message){
ResponseResult<T> respnotallow=new ResponseResult<>();
responseResult.setCode(code);
responseResult.setMessage(message);
return responseResult;
}
public static boolean isSucceed(ResponseResult responseResult){
return responseResult.getCode()==ResultCode.SUCCESS.getCode();
}
}
常用工具
除了這些基本的工具之外,我再推薦幾款我們項目中常用的工具
我們項目常常依賴中間件,比如mysql,kafka,redis等,如果要單元測試,我們通常的做法是在dev環境部署一套項目中依賴的中間件,非常麻煩,而且數據還不容易隔離,所以內存版的中間件就是來解決這個問題的。
內存版Redis:https://github.com/kstyrc/embedded-redis
內存版DB:https://github.com/mariadb
hutool:非常好用的java工具類庫 https://hutool.cn/
mybatis plus:非常好用的ORM框架 https://mapstruct.org/
mapStruct:java bean 映射工具 https://mapstruct.org/
redisson: 官方推薦客戶端 https://github.com/redisson/redisson