1.引入pom依賴
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
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啟動(dòng)類@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("本地測(cè)試",//大標(biāo)題
"",//小標(biāo)題
"1.0",//版本
"沒(méi)有服務(wù)條款",
new Contact("陽(yáng)Young", "", ""),//作者
"鏈接(無(wú))",//鏈接顯示文字
"網(wǎng)站鏈接"http://網(wǎng)站鏈接
);
return apiInfo;
}
}
3.controller
@RestController
@RequestMapping(value = "/student")
@Api(value = "student-api", description = "學(xué)生接口")
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.效果示例
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者