码迷,mamicode.com
首页 > 其他好文 > 详细

Swagger使用

时间:2020-07-29 09:53:01      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:turn   配置   rest   esc   spring   sep   ring   user   ppi   

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使用

标签:turn   配置   rest   esc   spring   sep   ring   user   ppi   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!