首页 > 其他 > 详细

Swagger使用

时间:2020-07-29 02:11:19      阅读:67      评论:0      收藏:0      [点我收藏+]

1. Maven依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

2. 配置类

@Configuration
@EnableSwagger2
public class Swagger {
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("PM Supply")
                .description("版本")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }
}

3. 示例

@Api(value = "用户管理类")
@RestController
@RequestMapping(value = "user")
public class UserController {

    @RequestMapping(value = "/user", method = RequestMethod.POST)
    @ApiOperation(value = "增加一个用户")
    public String addUser(@RequestBody User user) {
        return user.getName();
    }
}

4. 访问URL

http://localhost:8080/swagger-ui.html

@Api(value = "用户管理类")
@RestController
@RequestMapping(value = "user")
public class UserController {

@RequestMapping(value = "/user", method = RequestMethod.POST)
@ApiOperation(value = "增加一个用户")
public String addUser(@RequestBody User user) {
return user.getName();
}
}

Swagger使用

原文:https://www.cnblogs.com/oxygenG/p/13394638.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!