.yml配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useSSL=true
username: root
password: root
mvc:
path match:
matching-strategy: ANT_PATH_MATCHER
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:mapper/*.xml
server: port: 8088
mapper.xml头文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper></mapper>
knife4j 配置类
package com.lsm.demo4.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc; @Configuration @EnableSwagger2WebMvc public class Knife4jConfig{ @Bean(value = "docket") public Docket docket() { //指定使用Swagger2规范 Docket docket=new Docket(DocumentationType.SWAGGER_2) .apiInfo(new ApiInfoBuilder() .title("") //描述字段支持Markdown语法 .description("Knife4j RESTful APIs") .contact(new Contact("caocao","https://doc.xiaominfo.com/","xiaoymin@foxmail.com")) .version("1.0") .build()) //分组名称 .groupName("用户服务") .select() //这里指定Controller扫描包路径 .apis(RequestHandlerSelectors.basePackage("com.lsm.demo4.controller")) .paths(PathSelectors.any()) .build(); return docket; } }
validatad配置类
package com.lsm.demo4.config; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.validation.ObjectError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import java.util.List; @RestControllerAdvice public class ValidExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) public String exceptionHandler(MethodArgumentNotValidException exception) { BindingResult result = exception.getBindingResult(); StringBuilder stringBuilder = new StringBuilder(); if (result.hasErrors()) { List<ObjectError> errors = result.getAllErrors(); if (errors != null) { errors.forEach(p -> { FieldError fieldError = (FieldError) p; stringBuilder.append(fieldError.getDefaultMessage()); }); } } return stringBuilder.toString(); } }
需要的maven导入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.4.3</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>