SpringBoot之Swagger2

swagger版本

  • com.mangofactory

    com.mangofactory
    swagger-springmvc
    1.0.2
  • io.springfox
2.2.2

    io.springfox
    springfox-swagger-ui
    ${springfox.version}


    io.springfox
    springfox-swagger2
    ${springfox.version}

开启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 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 = "[email protected]";
        ApiInfo apiInfo = new ApiInfo(title,description,version,
                "terms of service", contact, "", "");
        return apiInfo;
    }
}

domain注解

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配置

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

    private ConcurrentHashMap 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";
    }

}

启动后访问
http://localhost:8080/swagger-ui.html

SpringBoot之Swagger2_第1张图片

参考

  • spring boot之使用springfox swagger展示restful的api doc

你可能感兴趣的:(java,springboot,swagger)