今天不太想写代码,于是整理了一个简单入门项目的基本结构,希望给想刚入门的人一些帮助
话不多说,先看项目依赖和项目结构
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
1.18.12
com.github.xiaoymin
knife4j-spring-boot-starter
2.0.1
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.apache.maven.plugins
maven-compiler-plugin
1.8
UTF-8
org.springframework.boot
spring-boot-maven-plugin
2.2.6.RELEASE
该项目创建方式是,使用start.aliyun.com基础模版创建,阿里刚开放的spring cloud生态,里面有非常多的组件,本工程简单,我就没有引用,大家感兴趣可以去了解学习下。
二、项目配置了统一参数校验
/**
* @author pillar
* @desc
* @email: [email protected]
* @createDate 2020/04/17 14:55
*/
@ApiModel("测试返回对象")
@Data
@ToString
public class PillarVo implements Serializable {
@Range(min = 6,max = 30,message = "年龄范围6-30")
@ApiModelProperty(value = "年龄",required = true,name = "age")
private int age;
@ApiModelProperty(value = "密码",required = true,name = "password")
@Size(min = 8,max = 24,message = "密码在长度8-24")
@Pattern(regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{8,24}$",message = "至8至24个字符,至少1个大写字母,1个小写字母和1个数字")
private String password;
@NotNull(message = "姓名不能为空")
@ApiModelProperty(value = "姓名",required = true,name = "name")
private String name;
@ApiModelProperty("附加属性")
private String head;
}
/**
* @author pillar
* @desc
* @email: [email protected]
* @createDate 2020/04/17 14:14
*/
@RestController
@Api(tags = "测试")
@AllArgsConstructor
@Slf4j
public class BaseController {
private final ObjectMapper objectMapper;
@ApiOperation("新增用户信息")
@PostMapping("save")
public PillarVo save(@RequestBody @Valid PillarVo pillarVo) throws JsonProcessingException {
log.info("pillar json=[]",objectMapper.writeValueAsString(pillarVo));
log.info("pillar=[]",pillarVo);
return pillarVo;
}
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseData methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
return new ResponseData<>(CodeEnum.PARAM_ERROR.getCode(),e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
}
/**
* @author pillar
* @desc
* @email: [email protected]
* @createDate 2020/04/17 14:14
*/
@RestControllerAdvice(basePackages = {"com.pillar.baseframework.controller"})
public class ResponseControllerAdvice implements ResponseBodyAdvice
自定义ApiException,规范了返回数据格式的一致性
/**
* @author pillar
* @desc
* @email: [email protected]
* @createDate 2020/04/17 15:13
*/
@Getter
public class ApiException extends RuntimeException {
private int code;
private String message;
public ApiException(int code, String message) {
this.code = code;
this.message = message;
}
public ApiException(String message) {
this.code = 500;
this.message = message;
}
public ApiException(CodeEnum codeEnum) {
this.code = codeEnum.getCode();
this.message = codeEnum.getMessage();
}
}
@ExceptionHandler(ApiException.class)
public ResponseData apiExceptionHandler(ApiException e) {
return new ResponseData(e.getCode(),e.getMessage());
}
/**
* @author pillar
* @desc
* @email: [email protected]
* @createDate 2020/04/17 14:14
*/
@Configuration
public class CrossConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
}
/**
* @author pillar
* @desc
* @email: [email protected]
* @createDate 2020/04/17 14:14
*/
@Controller
public class NotFound404 implements ErrorController {
@Override
public String getErrorPath() {
return "/error";
}
@RequestMapping(value = {"/error"})
@ResponseBody
public ResponseData error() {
return new ResponseData<>(404,"迷路了");
}
}
knife4j完全兼容swagger,在swagger基础上,开发出来一套更美丽的皮肤,以及其他高级功能,比如直接导出文档到html,pdf,markdown等等,非常好用
对于刚入门的你,如果对你有所帮助,希望来个小爱心,谢谢
demo地址https://gitee.com/yaochengzhu/base-framework.git