今天不太想写代码,于是整理了一个简单入门项目的基本结构,希望给想刚入门的人一些帮助 话不多说,先看项目依赖和项目结构 该项目创建方式是,使用start.aliyun.com基础模版创建,阿里刚开放的spring cloud生态,里面有非常多的组件,本工程简单,我就没有引用,大家感兴趣可以去了解学习下。 二、项目配置了统一参数校验 自定义ApiException,规范了返回数据格式的一致性 对于刚入门的你,如果对你有所帮助,希望来个小爱心,谢谢
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.2.6.RELEASE</version> </plugin> </plugins> </build>
一、项目依赖
/** * @author pillar * @desc * @email: 347637454@qq.com * @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: 347637454@qq.com * @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<String> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) { return new ResponseData<>(CodeEnum.PARAM_ERROR.getCode(),e.getBindingResult().getAllErrors().get(0).getDefaultMessage()); }
三、统一返回数据处理
/** * @author pillar * @desc * @email: 347637454@qq.com * @createDate 2020/04/17 14:14 */ @RestControllerAdvice(basePackages = {"com.pillar.baseframework.controller"}) public class ResponseControllerAdvice implements ResponseBodyAdvice<Object> { @Autowired private ObjectMapper objectMapper; @Override public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> aClass) { return !returnType.getParameterType().equals(ResponseData.class); } @Override public Object beforeBodyWrite(Object data, MethodParameter returnType, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest request, ServerHttpResponse response) { return new ResponseData<>(data); } }
四、自定义异常处理
/** * @author pillar * @desc * @email: 347637454@qq.com * @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: 347637454@qq.com * @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); } }
六、404访问处理
/** * @author pillar * @desc * @email: 347637454@qq.com * @createDate 2020/04/17 14:14 */ @Controller public class NotFound404 implements ErrorController { @Override public String getErrorPath() { return "/error"; } @RequestMapping(value = {"/error"}) @ResponseBody public ResponseData<String> error() { return new ResponseData<>(404,"迷路了"); } }
七、knife4j的使用
knife4j完全兼容swagger,在swagger基础上,开发出来一套更美丽的皮肤,以及其他高级功能,比如直接导出文档到html,pdf,markdown等等,非常好用
八、弃用tomcat容器,使用更高性能的undertown或者netty
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算