提问人:Nitish K 提问时间:11/10/2023 最后编辑:Nitish K 更新时间:11/12/2023 访问量:33
我们如何在 Swagger 中修改控制器 API 请求 URL?
How do we modify Controller API request URL in Swagger?
问:
我正在研究Java Spring MVC项目的Swagger实现。(NON-Spring Boot 项目,在 tomcat 9.0v 上手动部署)
URL for Swagger : http://localhost:8080/kes/v1.0/swagger-ui/index.html
因此,当点击任何控制器 API 时,Swagger 形成的请求 URL 是这样的
Actual : http://localhost:8080/v1.0/kes/swagger-ui/index.html
Expected : http://localhost:8080/kes/v1.0/swagger-ui/index.html
actual /v1.0/kes/
expected /kes/v1.0/
由于这个问题,我收到 404 错误。 我尝试了多个选项,包括更改web.xml中的URL模式,但没有解决。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
<!--scope>compile</scope-->
</dependency>
配置是这样的
package com.pen.lis.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
}
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.eds.kes.controller")).build();
}
}
答:
0赞
Shuchi Sinha
11/10/2023
#1
您能否更改 Swagger 配置中的 URL 模式以匹配 API 端点的实际 URL,然后重新启动 tomcat。
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/kes/v1.0/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
}
评论
0赞
Nitish K
11/12/2023
它不起作用,它抛出错误 [http-nio-8080-exec-5] (DispatcherServlet.java:1283) - GET /kes/v1.0/swagger-ui/index.html 没有映射,也尝试了多种方案,没有一个有效,Swagger 在更改为 addResourceHandler(“/kes/v1.0/swagger-ui/**”) 时实际上不起作用
评论