一个SpringBoot结合Swagger写接口文档的例子

Swagger接入

在原有的SpringBoot中接入Swagger

第一步。

在pom.xml中添加maven依赖

 <!--swagger库-->
         <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-swagger2</artifactId>
             <version>2.8.0</version>
         </dependency>
         <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-swagger-ui</artifactId>
             <version>2.8.0</version>
         </dependency>
复制代码

第二步。

创建Swagger2配置类 , 代码如下. 请手动修改包名等信息

@Configuration
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //这里要改为自己项目包名
                .apis(RequestHandlerSelectors.basePackage("com.example.demo"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("在SpringBoot项目结合Swagger编写接口文档")
                .description("Swagger官方仓库https://github.com/swagger-api/swagger-ui")
                .termsOfServiceUrl("https://github.com/forgot2015/SpingBootAndSwaggerDemo")
                .version("1.1.0")
                .build();
    }
}
复制代码

第三步。

在你的Application类顶部添加注解 @EnableSwagger2 加完如下:

@SpringBootApplication
@EnableSwagger2
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
复制代码

第四步。

在Controller类中编写接口注解内容

添加类注解

@Api(value="/test", tags="Swagger测试接口模块")
复制代码

添加请求方法注解

@ApiOperation(value="根据Id查询用户信息", notes = "根据Id查询用户信息,这里是详细说明")
@ApiImplicitParam(name="id", value="用户唯一id", required = true, dataType = "Long")
复制代码

比如:

@Api(value="/test", tags="Swagger测试接口模块")
@RestController
@RequestMapping("/api")
public class UserController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;

    @ApiOperation(value="根据Id查询用户信息", notes = "根据Id查询用户信息,这里是详细说明")
    @ApiImplicitParam(name="id", value="用户唯一id", required = true, dataType = "Long")
    @RequestMapping(value="/user",params = "id",method = RequestMethod.GET)
    public User getUserById(@RequestParam Long id){
        return userService.findById(id);
    }

    @ApiOperation(value="根据Id添加用户信息", notes = "根据Id添加用户信息,这里是详细说明")
    @ApiImplicitParam(name="id", value="用户唯一id", required = true, dataType = "Long")
    @RequestMapping(value="/user",params = "id",method = RequestMethod.POST)
    public User addUser(@RequestParam Long id){
        return userService.addUser(id);
    }
}
复制代码

最后,访问http://localhost:8080/swagger-ui.html 即可看到你的杰作

具体完整demo见github仓库

https://github.com/forgot2015/SpingBootAndSwaggerDemo

更多内容阅读

https://www.jianshu.com/p/b0b19368e4a8

https://blog.csdn.net/xupeng874395012/article/details/68946676

转载于:https://juejin.im/post/5ae17024518825672c00a590

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值