SpringBoot2集成Swagger2配置及部署

SpringBoot2集成Swagger2配置及部署

项目中用到的,网上找了些资料,因为版本不一样,业务场景也不一样,很难直接用现成的,只能亲自去验证,并将步骤记录下来,在这里做个分享,而且swagger-ui在本地调试阶段可以访问,但当部署到服务器之后就报404了,这个也在网上找到了解决办法,也验证了,在这里一并记录下来。
springboot项目搭建过程可以参考我的这篇文博,url链接如下:

springboot+mybatis-plus+mysql项目完整搭建(十分详尽!)

1. 配置Maven依赖项

这里我用的是2.9.1版本。

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>2.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>2.9.1</version>
</dependency>
2. Swagger2配置类编写
  • SwaggerConfig 用来配置基本项
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.zhousonglin.mylearnlab.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("SpringBoot测试demo")
                        .description("mySpringBootLearnning测试站点")
                        .version("9.0")
                        .contact(new Contact("测试demo","cn.zhousonglin.mylearnlab","dahlinsky@qq.com"))
                        .license("The Apache License")
                        .licenseUrl("http://www.zhousongln.cn")
                        .build());
    }
}
  • WebApiConfig 用来配置 部署后找不到静态文件的问题
@Configuration
public class WebApiConfig extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH")
                .allowCredentials(true).maxAge(3600);
    }
}
3. 控制器中使用

Swagger 通过注解定制接口对外展示的信息,这些信息包括接口名、请求方法、参数、返回信息等。
常用的注解类型:

  • @Api:修饰整个类,描述Controller的作用
  • @ApiOperation:描述一个类的一个方法,或者说一个接口
  • @ApiParam:单个参数描述
  • @ApiModel:用对象来接收参数
  • @ApiProperty:用对象接收参数时,描述对象的一个字段
  • @ApiResponse:HTTP响应其中1个描述
  • @ApiResponses:HTTP响应整体描述
  • @ApiIgnore:使用该注解忽略这个API
  • @ApiError :发生错误返回的信息
  • @ApiImplicitParam:描述一个请求参数,可以配置参数的中文含义,还可以给参数设置默认值
  • @ApiImplicitParams:描述由多个 @ApiImplicitParam 注解的参数组成的请求参数列表

@RestController
@Api(tags = "用于测试的demo接口")
@RequestMapping("/test")
public class HomeController {

    @Autowired
    private IUserService userService;

    @GetMapping("/getAllUsers.json")
    @ApiOperation("测试获取全部用户")
    public @ResponseBody Object GetAllUsers(){
        return  userService.GetAllUsers();
    }
    
    @GetMapping("/{name}/{age}/getUser")
    @ApiOperation("获取用户接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", defaultValue = "王小红", required = true),
            @ApiImplicitParam(name = "age", value = "年龄", defaultValue = "18", required = true)
    })
    public @ResponseBody Object GetUser(@PathVariable String name, @PathVariable String age){
        return userService.GetUser(name,age);
    }

    @GetMapping("/{age}/getUsersAll")
    @ApiOperation("获取用户列表")
    @ApiImplicitParam(name = "age", value = "年龄", defaultValue = "18", required = true)
    public @ResponseBody Object GetUserAll(@PathVariable String age){
        return shortPathService.GetUserAll(age);
    }
}

运行后,浏览器输入 http://127.0.0.1:8080/swagger-ui.html ,其中端口号可以自定义,默认端口号是8080。

4. 文档及引用链接
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值