Spring Boot Swagger2 example 2017-11-06 04:46

Swagger can help you generate document for your restful API. This blog will show you how to use it. I assume that you have known how to use Spring Boot. The structure of example project is like following.

│  ├─java
│  │  └─com
│  │      └─henryxi
│  │          └─swagger2
│  │                  AppConfig.java
│  │                  SimpleController.java
│  │                  User.java
│  │
│  └─resources

the content of pom.xml file.



public class AppConfig extends WebMvcConfigurerAdapter {
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
    public static void main(String[] args) throws Exception {
        SpringApplication.run(AppConfig.class, args);


@Api(description = "simple controller")
public class SimpleController {

    @RequestMapping(value = "/get-user-info", method = RequestMethod.GET)
    @ApiOperation(value = "get user info")
    public User userInfo() {
        User user = new User();
        return user;

    @RequestMapping(value = "/post-user-info", method = RequestMethod.POST)
    @ApiOperation(value = "save user info")
    public User userInfo(@RequestBody User user) {
        System.out.println("user info:" + user);
        return user;


public class User implements Serializable {
    private String name;
    private int age;
    private String department;

    //getter and setter methods

Access http://localhost:8080/swagger-ui.html#/, you will see the document of restful APIs.