1. Processus d'intégration
1. Introduire des dépendances
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.5.1</version>
</dependency>
2. Écrivez une classe de configuration swagger
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.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@Configuration
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.huawei.ssmdemo2023"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("华为昆明数字经济学院 实验项目")
.termsOfServiceUrl("localhost:8080/ssm")
.description("springmvc swagger2")
.contact(new Contact("Gaojingbo", "http://www.baidu.com", "[email protected]"))
.version("1.1")
.build();
}
}
3. Configurer le mappage du répertoire de ressources statiques Swagger
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"></mvc:mapping>
<mvc:exclude-mapping path="/user/doLogin" />
<mvc:exclude-mapping path="/reqCode/getReqCode" />
<mvc:exclude-mapping path="/swagger-ui.html/**" />
<mvc:exclude-mapping path="/webjars/**" />
<mvc:exclude-mapping path="/swagger-resources/**" />
<mvc:exclude-mapping path="/v2/**" />
<bean class="com.huawei.ssmdemo2023.core.interceptors.LoginInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
4. Ajouter des annotations aux classes et méthodes du contrôleur
@Api(description = "学生信息管理模块")
@Controller
@RequestMapping("/student")
@Slf4j
public class StudentController {
@Resource(name = "studentServiceImpl")
private StudentService studentService;
@ApiOperation(value = "获取学生信息通过学生id(GET)", notes = "查询记录:http method is get")
@RequestMapping(value = "/getStudentByID",method = RequestMethod.GET)
@ResponseBody
public MyResult getStudentByID(@ApiParam(required = true, hidden = false, value = "学生id") String id){
log.debug("学生信息管理:获取学生信息通过学生id [param:id"+id+"]");
MyResult result = new MyResult();
StudentDO studentDO = studentService.getStudentByID(id);
if(studentDO==null){
result.setCode(500);
result.setMsg("没有查询到任何学生数据!");
}else{
result.setMsg("学生信息查询成功!");
result.setData(studentDO);
}
return result;
}
}