2

swagger版本

  • 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>

开启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注解

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

}

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

clipboard.png

参考


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...