1、Maven依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.5.4
com.zeekrlife.jira
mcp-tool
0.0.1-SNAPSHOT
jira-app
Demo project for Spring Boot
1.8
UTF-8
UTF-8
org.springframework.boot
spring-boot-starter-web
io.springfox
springfox-boot-starter
3.0.0
com.github.xiaoymin
knife4j-spring-boot-starter
3.0.2
org.springframework.boot
spring-boot-maven-plugin
2、Swagger配置
/**
* 访问路径ip:port/doc.html
*/
@Configuration
//@EnableOpenApi
@EnableKnife4j
public class SwaggerConfig {
/**
* 用于读取配置文件 application.properties 中 swagger 属性是否开启
*/
@Value("${swagger.enabled:true}")
private Boolean swaggerEnabled;
/**
* 生成通用响应信息
*/
private List getGlobalResponseMessage() {
List responseList = new ArrayList();
responseList.add(new ResponseBuilder().code("404").description("找不到资源").build());
return responseList;
}
/**
* 设置授权信息
*/
private List securitySchemes() {
ApiKey apiKey = new ApiKey("tfToken", "token", "header");
return Collections.singletonList(apiKey);
}
@Bean
public Docket docket() {
// OAS_30:区别于 V2,(OpenAPI Specification 的简称 OAS)
return new Docket(
// 使用 OpenAPI 3.0
DocumentationType.OAS_30)
.enable(swaggerEnabled)
// API 信息
.apiInfo(apiInfo())
// API 分组
//.groupName(swaggerProperties.getGroupName())
.select()
// 对某个包的接口进行监听
.apis(RequestHandlerSelectors.basePackage("com.dsf.mcp.controller"))
// 监听所有接口
// .apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
//.globalRequestParameters(getGlobalRequestParameters())
.globalResponses(HttpMethod.GET, getGlobalResponseMessage())
.globalResponses(HttpMethod.POST, getGlobalResponseMessage())
.globalResponses(HttpMethod.DELETE, getGlobalResponseMessage())
.globalResponses(HttpMethod.PUT, getGlobalResponseMessage())
//添加登录认证
.securitySchemes(securitySchemes()) ;
}
private ApiInfo apiInfo() {
/*作者信息*/
Contact contact = new Contact("Jira", "https://demo.com", "123@qq.com");
return new ApiInfoBuilder()
// 文档标题
.title("能力门户")
// 文档描述
.description("能力门户")
// 联系人信息
.contact(contact)
// 文档版本
.version("1.0")
.build();
}
}
3、Spring MVC配置
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
.resourceChain(false);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/swagger-ui/")
.setViewName("forward:/swagger-ui/index.html");
}
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net