SpringBoot 集成 Swagger 2教程
首先你需要有个 SpringBoot 项目
pom 文件
<properties>
<java.version>1.8</java.version>
<swagger2.version>2.9.2</swagger2.version>
<swagger-ui.version>2.9.2</swagger-ui.version>
<!--由于 swagger 版本冲突 guava 更新至20.0-->
<guava.version>20.0</guava.version>
</properties>
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger2.version}</version>
</dependency>
<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger-ui.version}</version>
</dependency>
Java配置文件
package com.zjrc.healthlife.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Description: swagger 配置
* User: zhouzhou
* Date: 2019-03-28
* Time: 8:43 AM
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket swaggerSpringMvcPlugin() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors
.withMethodAnnotation(ApiOperation.class)).build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("Health-File")
.description("健康导航 api")
.termsOfServiceUrl("浙江融创")
.version("1.0.0")
.build();
}
}
测试专用 Controller
package com.zjrc.healthlife.controller;
import com.zjrc.healthlife.common.vo.Response;
import com.zjrc.healthlife.dao.entity.Patient;
import com.zjrc.healthlife.dao.mapper.PatientMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* Description: 测试用
* User: zhouzhou
* Date: 2019-03-27
* Time: 9:00 PM
*/
@Api(description = "测试用")
@RestController
public class TestController {
@Autowired
private PatientMapper patientMapper;
@ApiOperation(value = "查询用户用 id" , notes="测试查询")
@GetMapping("/patient/{id}")
public Response<Patient> getPatientById(@PathVariable Long id){
Response<Patient> response = new Response<>();
Patient patient = patientMapper.selectByPrimaryKey(id);
if (patient == null){
response.setCode(Response.HttpResponseStatus.ERR);
response.setMsg("查无此人");
}else {
response.setCode(Response.HttpResponseStatus.OK);
response.setData(patient);
}
return response;
}
@ApiOperation(value = "测试全局异常" , notes="测试异常")
@GetMapping("/getError")
public Response<String> getError(){
Response<String> response = new Response<>();
throw new RuntimeException("我是一个测试异常");
}
}
启动后,访问{ip}/swagger-ui.html