1.添加依赖
springboot 项目添加maven依赖,修改pom.xml
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!-- spring boot 基础依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2.添加swagger配置类 SwaggerConfig.java
- 修改 package com.lij.springbootdemo.config;
- 修改 com.lij.springbootdemo.controller
package com.lij.springbootdemo.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.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.lij.springbootdemo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swagger 构建RESTful API")
.description("")
.termsOfServiceUrl("")
//.contact(contact)
.version("1.0")
.build();
}
}
3.编写接口
实体类
package com.lij.springbootdemo.entity;
import java.io.Serializable;
import lombok.Data;
@Data
public class PersonInfo implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String gender;
private String nationality;
private String idcardNumber;
private String birthDay;
private String address;
}
controller
package com.lij.springbootdemo.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.lij.springbootdemo.entity.PersonInfo;
@RestController
@RequestMapping("/person")
@Api(tags = "人员接口",description="人员文档说明",hidden=true)
public class PersonController {
@RequestMapping(value="selectAll",method=RequestMethod.POST)
@ApiOperation(value="查询所有的人员",notes="查询所有的人员接口说明")
@ApiImplicitParams({
@ApiImplicitParam(name="id",value="",dataType="String", paramType = "query"),
@ApiImplicitParam(name="pageSize",value="页码",dataType="String", paramType = "query"),
@ApiImplicitParam(name="pageNum",value="每页条数",dataType="String", paramType = "query"),
})
@ApiResponse(response=PersonInfo.class, code = 200, message = "接口返回对象参数")
public List<PersonInfo> selectAll(HttpServletRequest request) {
List<PersonInfo> list = new ArrayList<PersonInfo>();
return list;
}
@RequestMapping(value="findById",method=RequestMethod.GET)
@ResponseBody
public PersonInfo findById(Integer id) {
return new PersonInfo();
}
}
4.入口类加注解@EnableSwagger2
5.访问swagger页面
http://localhost:8080/swagger-ui.html
坑点
- 千万不要尝试非 spring boot 集成 swaggerUI,坑点很多(莫名其妙报错很多,而且网上找不到答案,特别是开发中的springMVC项目集成swagger)
- 可以尝试将原项目改成spring boot项目,在加入依赖(待尝试)
- 访问swagger页面报错弹出框(Unable to infer base url.)解决方案---清除maven缓存,重新下载maven--update project