Java 当我试图打开swagger-ui.html时,我得到了404
我没有SpringBoot,只有SpringMVC和安全性。 我的配置:Java 当我试图打开swagger-ui.html时,我得到了404,java,spring-mvc,spring-security,swagger,Java,Spring Mvc,Spring Security,Swagger,我没有SpringBoot,只有SpringMVC和安全性。 我的配置: @Configuration @EnableTransactionManagement @EnableWebMvc @EnableSwagger2 @PropertySource("classpath:access.properties") @ComponentScan(basePackages = {"com.company.service", "com.company.
@Configuration
@EnableTransactionManagement
@EnableWebMvc
@EnableSwagger2
@PropertySource("classpath:access.properties")
@ComponentScan(basePackages = {"com.company.service", "com.company.dao", "com.company.controller"})
public class SpringConfig extends WebMvcConfigurationSupport {
@Value("${url}")
private String URL;
@Value("${user}")
private String USER;
@Value("${password}")
private String PASSWORD;
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler( "swagger-ui.html" )
.addResourceLocations( "classpath:/META-INF/resources/" );
registry.addResourceHandler( "/webjars/**" )
.addResourceLocations( "classpath:/META-INF/resources/webjars/" );
}
........
pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
许多文章讨论了使用Spring Boot或添加注释来解决此问题的可能方法,但在我的情况下这是没有用的。尝试以下方法:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler( "api/swagger-ui.html" )
.addResourceLocations( "classpath:/META-INF/resources/" );
}
或者删除
/api/
当您想要访问您的文档时我认为您需要将程序包添加到控制器所在的位置,这样它将被swagger扫描:。api(RequestHandlerSelectors.basePackage(“您的程序包”)
这里有一个例子
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket getDocketInstance() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("Spring Boot project")
.description("Spring Boot bootstrap project")
.version("0.1")
.license("Unlicense")
.build())
.select()
.apis(RequestHandlerSelectors.basePackage("your package"))
.paths(PathSelectors.any())
.build();
}
/api/
是我的应用程序的根url,很抱歉,这没有帮助对不起,但这也没有帮助与RequestHandlerSelectors有什么区别。any()
,是。any()
不会拦截所有请求吗?如果有问题,我不会从http://localhost:8080/api/v2/api-文档
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket getDocketInstance() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("Spring Boot project")
.description("Spring Boot bootstrap project")
.version("0.1")
.license("Unlicense")
.build())
.select()
.apis(RequestHandlerSelectors.basePackage("your package"))
.paths(PathSelectors.any())
.build();
}