1.pom.xml

<!--swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--下面的不添加会导致访问报404-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<!--swagger2密码-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
application.properties添加swagger访问密码:

security.basic.enabled=true
security.basic.path=/swagger-ui.html
security.user.name=admin
security.user.password=123456

2.swagger配置类

参考:http://blog.didispace.com/springbootswagger2/

3.生成离线文档:

使用springfox-staticdocs生成swagger离线api文档附带源码

4.项目使用demo:

responseMessages.add(new ResponseMessageBuilder().code(999).message("未知异常").responseModel(new ModelRef(error)).build());
return
new Docket(DocumentationType.SWAGGER_2).
apiInfo(buildApiInfo())
.tags(new Tag("tag1", "接口类描述1"),
new Tag("tag2", "接口类描述2")).
select().
apis(RequestHandlerSelectors.
basePackage(swaggerBasePackage)).
paths(PathSelectors.any()).
build()
.useDefaultResponseMessages(false)
.globalResponseMessage(RequestMethod.GET, responseMessages)
@Api(tags = {"tag1"})
@RestController
@RequestMapping(value = "test")
public class ResourceApi { //...some code... /**
* @author Jill
*/
@PostMapping
@ApiOperation("查询资源列表接口")
public Response<Object> getInfoList(@RequestBody @Valid
@ApiParam(name = "查询条件", value = "查询条件")
SearchVO vo) {
04-14 05:31