1. 程式人生 > >springboot(6)--整合swagger

springboot(6)--整合swagger

個人覺得swagger有點類似於阿里的hsf ops,只不過hsf ops是用於線上測試分散式服務,而swagger是用於線上測試rest api,感覺springboot整合swagger需要好多註解。。。。

0.在pom檔案引入依賴

<dependency>
    <groupId>io.springfox</groupId>
    <artifact>springfox-swagger2</artifact>
    <version>2.8.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifact>springfox-swagger-ui</artifact>
    <version>2.8.0</version>
</dependency>

1.引入配置

@EnableSwagger2
@Configuration
public class SwaggerConfig {
 
    //是否開啟swagger,正式環境一般是需要關閉的,可根據springboot的多環境配置進行設定
    @Value(value = "${swagger.enabled}")
    Boolean swaggerEnabled;
 
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                // 是否開啟
                .enable(swaggerEnabled).select()
                // 掃描的路徑包
                .apis(RequestHandlerSelectors.basePackage("cn.lqdev.learning.springboot.chapter10"))
                // 指定路徑處理PathSelectors.any()代表所有的路徑
                .paths(PathSelectors.any()).build().pathMapping("/");
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot-Swagger2整合和使用-demo示例")
                .description("qinlo| 趔趄的猿")
                // 作者資訊
                .contact(new Contact("oKong", "https://blog.csdn.net/qq_30186661", "
[email protected]
")) .version("1.0.0") .build(); } }

3.在要測試的介面添加註解

@RestController
@RequestMapping(value ="/users")
@Api(tags = "使用者API")
public class UserController {
    static Map<Long,User> users= Collections.synchronizedMap(new HashMap<Long,User>());

    @RequestMapping(value="/", method= RequestMethod.GET)
    @ApiOperation(value = "獲取使用者列表")
    public List<User> getUserList(){
        List<User>  list=new ArrayList<User>(users.values());
        return list;
    }

    @RequestMapping(value="/",method = RequestMethod.POST)
    @ApiOperation(value = "新增使用者")
    public String postUser(@
[email protected]
User user){ users.put(user.getId(),user); return "success"; } @RequestMapping(value = "/{id}", method = RequestMethod.GET) @ApiOperation(value = "獲取使用者") public User getUser(@PathVariable Long id){ return users.get(id); } @ApiOperation(value = "修改使用者") @RequestMapping(value = "/{id}", method = RequestMethod.PUT) @ApiImplicitParam(name = "id", value = "查詢使用者id", required = true) public String putUser(@PathVariable Long id, @[email protected] User user){ User u=users.get(user.getId()); u.setName(user.getName()); u.setAge(user.getAge()); users.put(user.getId(),u); return "success"; } @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) @ApiOperation(value = "刪除使用者") public String deleteUser(@PathVariable Long id){ users.remove(id); return "success"; } }

User.java

@ApiModel
public class User {
    @NotEmpty
    @ApiModelProperty(value = "id",dataType = "String",name="id",example = "111111")
    private Long id;
    @NotEmpty
    @ApiModelProperty(value = "name",dataType = "String",name="name",example = "lq")
    private String name;
    @NotEmpty
    @ApiModelProperty(value = "age",dataType = "int",name="age",example = "12")
    private int age;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}