结构
代码:
https://github.com/Ciel1998/swagger
效果
pom
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
SpringFoxConfig
@Configuration
@EnableSwagger2
public class SpringFoxConfig {
@Bean
public Docket apiDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
// .apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.apis(RequestHandlerSelectors.basePackage("com.liziyi.tang"))
// .paths(PathSelectors.ant("/v2/**"))
.build()
.apiInfo(getApiInfo());
}
private ApiInfo getApiInfo() {
return new ApiInfo(
"liziyi的swagger",
"描述暂无",
"V1.0",
"http://www.baidu.com",
new Contact("liziyi", "http://www.baidu.com", "[email protected]"),
"LICENSE",
"http://www.baidu.com",
Collections.emptyList()
);
}
}
controller
@EnableSwagger2
@RestController
@RequestMapping("/v2/persons/")
@Api(description = "Set of endpoints for Creating, Retrieving, Updating and Deleting of Persons.")
public class PersonController {
@RequestMapping(method = RequestMethod.GET, produces = "application/json")
@ApiOperation("Returns list of all Persons in the system.")
public String getAllPersons() {
return "wagger";
}
}