springboot(7)--集成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", "852925993@qq.com"))
                .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(@Valid@ModelAttribute 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, @Valid@ModelAttribute 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;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值