As time goes by, it is necessary to modify interface documents synchronously when modifying interface implementation. Documents and codes are in two different media. Unless there is a strict management mechanism, it is easy to lead to inconsistencies.
Adding Swagger2 dependencies
Adding swagger 2 dependencies to pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
Create swagger2 configuration
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.caiyi.financial.nirvana"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot Use in Swagger2 structure RESTful APIs")
.description("this id description")
.termsOfServiceUrl("http://blog.csdn.net/eacter")
.contact("myName")
.version("1.0")
.build();
}
}
spring load configuration is enabled through the @Configuration annotation, @Enable Swagger2 enables Swagger2
After creating Docket beans through the createRestApi function, apiInfo() is used to create the basic information of the Api (which is displayed on the document page). The select() function returns an ApiSelector Builder instance to control which interfaces are exposed to Swagger for presentation. This example is defined by a specified scan package path. Swagger scans all APIs defined by Controller under the package and generates document content (except for requests specified by @ApiIgnore).
Add document content
@Api(value = "Swagger Dynamic document demo")
@RestController
@RequestMapping(value="/demo/users")
public class SwaggerDemoController {
static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());
@ApiOperation(value="Get the user list", notes="")
@RequestMapping(value={""}, method= RequestMethod.GET)
public List<User> getUserList() {
List<User> r = new ArrayList<User>(users.values());
return r;
}
@ApiOperation(value="Create user", notes="according to User Object Creation User")
@ApiImplicitParam(name = "user", value = "User Detailed Entities user", required = true, dataType = "User")
@RequestMapping(value="", method=RequestMethod.POST)
public String postUser(@RequestBody User user) {
users.put(user.getId(), user);
return "success";
}
@ApiOperation(value="Getting User Details", notes="according to url Of id To get user details")
@ApiImplicitParam(name = "id", value = "user ID", required = true, dataType = "Long")
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public User getUser(@PathVariable Long id) {
return users.get(id);
}
@ApiOperation(value="Update user details", notes="according to url Of id To specify the update object, and according to the passed user Information updates user details")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "user ID", required = true, dataType = "Long"),
@ApiImplicitParam(name = "user", value = "User Detailed Entities user", required = true, dataType = "User")
})
@RequestMapping(value="/{id}", method=RequestMethod.PUT)
public String putUser(@PathVariable Long id, @RequestBody User user) {
User u = users.get(id);
u.setCname(user.getCname());
u.setAge(user.getAge());
users.put(id, u);
return "success";
}
@ApiOperation(value="delete user", notes="according to url Of id To specify deletion objects")
@ApiImplicitParam(name = "id", value = "user ID", required = true, dataType = "Long")
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public String deleteUser(@PathVariable Long id) {
users.remove(id);
return "success";
}
}
API Document Access and Debugging
Complete the above code addition, start Spring Boot program, access: http://localhost:8080/swagger-ui.html
. You can see the page of the generated RESTful API.
Open specific API requests, Try it out!
Live Demo http://petstore.swagger.io/
Document Data Address
http://127.0.0.1:8080/v2/api-docs
Common problem
Not accessible to swagger-ui
Find it on GitHub Swagger-ui Item: https://github.com/swagger-api/swagger-ui Copy all content under dist to the local project web project and modify index.html
//url = "http://petstore.swagger.io/v2/swagger.json"; url = "http://localhost:8090/v2/api-docs";
reference information
Swagger-UI official website
Spring boot 2.0 - swagger 2 integrates swagger-ui.html
github: swagger-ui