SpringBoot集成SwaggerUI

1.引入pom依赖
  
    
        io.springfox
        springfox-swagger2
        2.5.0
    
    
    
        io.springfox
        springfox-swagger-ui
        2.5.0
    
2.config类
package com.young.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;

/**
 * @ClassName SwaggerConfig
 * @Description: swagger配置
 * @Author Young
 * @Date 2019/7/23
 * @Version V1.0
 **/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    /**
     * 方法1:直接扫描包名路径
     * 方法2:在Application启动类@SpringBootApplication注解加 scanBasePackages = "com.young"
     * @return
     */
    @Bean
    public Docket api(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2).
                groupName("接口API").
                genericModelSubstitutes(DeferredResult.class).
                useDefaultResponseMessages(false).
                forCodeGeneration(true).
                select().
//              apis(RequestHandlerSelectors.basePackage("com.young")).
                apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).
                build().
                apiInfo(apiInfo());
        return docket;
    }
    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo("本地测试",//大标题
                "",//小标题
                "1.0",//版本
                "没有服务条款",
                new Contact("陽Young", "", ""),//作者
                "链接(无)",//链接显示文字
                "网站链接"//网站链接
        );
        return apiInfo;
    }
}

3.controller
@RestController
@RequestMapping(value = "/student")
@Api(value = "student-api", description = "学生接口")
public class StudentController {

    @Autowired
    private CryptService cryptService;

    @ApiOperation(value = "查找")
    @RequestMapping(value = "/query", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseBody
    public StudentRespVO query(@RequestBody @Validated StudentReqVO request) {
        return cryptService.crypt(request);
    }
}
4.效果示例
SpringBoot集成SwaggerUI_第1张图片
示例1.png

你可能感兴趣的:(SpringBoot集成SwaggerUI)