Swagger2 of SpringBoot

  java, springboot, swagger

Swagger version

  • com.mangofactory
<dependency>
    <groupId>com.mangofactory</groupId>
    <artifactId>swagger-springmvc</artifactId>
    <version>1.0.2</version>
</dependency>
  • io.springfox
<springfox.version>2.2.2</springfox.version>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${springfox.version}</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${springfox.version}</version>
</dependency>

Open swagger2

/**
 * Created by codecraft on 2016-05-05.
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select().paths(paths()).build().apiInfo(apiInfo());
    }

    @SuppressWarnings("unchecked")
    private Predicate<String> paths() {
        return Predicates.or(Predicates.containsPattern("/api/*"));
    }

    private ApiInfo apiInfo() {
        String title = "my api";
        String description = "api desc";
        String version = "1.0.0-snapshot";
        String contact = "codecraft@gmail.com";
        ApiInfo apiInfo = new ApiInfo(title,description,version,
                "terms of service", contact, "", "");
        return apiInfo;
    }
}

Domain annotation

package com.codecraft.domain;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * Created by codecraft on 2016-05-05.
 */
@ApiModel
public class User {

    @ApiModelProperty(value = "User ID")
    private String id;

    @ApiModelProperty(value = "first name")
    private String firstName;

    @ApiModelProperty(value = "last name")
    private String lastName;

    public User() {
    }

    public User(String id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
}

Controller configuration

@RestController
@RequestMapping("/api/user")
public class UserController {

    private ConcurrentHashMap<String,User> userMap = new ConcurrentHashMap<>();

    @RequestMapping(method = RequestMethod.POST)
    public String add(@RequestBody
            @ApiParam(name = "user",value = "json format",required = true)
                          User user){
        userMap.put(user.getId(),user);
        return "add successfully";
    }

    @RequestMapping(method = RequestMethod.PUT)
    public String update(@RequestBody
                             @ApiParam(name = "user",value = "json format",required = true)
                             User user) {
        userMap.put(user.getId(),user);
        return "update successfully";
    }

    @RequestMapping(method = RequestMethod.GET)
    public User get(@RequestParam(value = "id", required = true) String id){
        return userMap.get(id);
    }

    @RequestMapping(method = RequestMethod.DELETE)
    public String delete(@RequestParam(value = "id", required = true) String id){
        userMap.remove(id);
        return "delete successfully";
    }

}

Post-launch access
http://localhost:8080/swagger-ui.html

clipboard.png

References