Spring Boot集成Swagger应对前后端联调

蓝咒 提交于 2019-12-09 13:52:49

  现在的项目大部分都是前后端分离的,联调的时候沟通成本是个问题,而Swagger能很好地减少沟通成本,下面讲解Spring Boot如何集成Swagger。

Maven:

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

新建Swagger2文件(跟启动类在同一个目录):

@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createdRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("controller"))
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("测试API")
                .contact(new Contact("manonglixianji", "", ""))
                .version("1.0")
                .description("测试接口")
                .build();
    }
}

接着在Controller层的方法中集成Swagger就行了:

@RequestMapping("/test")
@RestController
@Api("码农历险记api")
public class testController {

    @Autowired
    private TestService testService;

    /**
     * 测试列表
     * @param date
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询列表",notes = "分页查询操作")
    @ApiImplicitParams({ @ApiImplicitParam(value = "页数",name = "page",dataType = "int",paramType = "path"),
            @ApiImplicitParam(value = "页数大小",name = "size",dataType = "int",paramType = "path"),
            @ApiImplicitParam(value = "日期",name = "date",dataType = "string",paramType = "path")})
    public Page<ReturnVO> findDormCodeList(@PageableDefault Pageable pageable,
                                           @RequestParam(value = "date",required = false) String date) {
        return testService.findTestList(pageable,date);
    }
}

实体类ReturnVO

最后启动我们的项目在浏览器输入:http://localhost:8080/swagger-ui.html 结果就出来了。

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!