这是我的第一篇文章,所以随时留下一些反馈或如果我做错了:)
我正在使用spring-boot和resteasy:
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.paypal.springboot</groupId>
<artifactId>resteasy-spring-boot-starter</artifactId>
<version>2.3.4-RELEASE</version>
</dependency>
我正在尝试使用Swagger来查看我的端点,因此我添加了此依赖项:
<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>
此依赖项已加载到我的仓库中,一切看起来都很好。
我添加了该类,以使其成为最简单的配置类:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
当我在调试模块中启动应用程序时,我输入了先前的Bean。 Evrything看起来不错。
当我启动Spring应用程序时:
@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class,RabbitAutoConfiguration.class})
public class SituationServicesApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SituationServicesApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
}
}
一切都很好:
2019-07-06 15:43:27.222 INFO 6336 --- [ main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [v2/api-docs] onto method [public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]
但是当我尝试到达swagger-ui.html时:
http://localhost:8092/test/v2/api-docs
或
http://localhost:8092/test/swagger-ui.html
我遇到404错误:
“ RESTEASY003210:找不到完整路径的资源: http://localhost:8092/test/v2/api-docs”。
我尝试通过添加以下属性来修改默认网址:springfox.documentation.swagger.v2.path=v2/api-docs-test
静止404。
我从头开始尝试了一个新的空项目,它工作得很好。我猜我的项目出了点问题。
我确定使用的网址。
您知道如何调试此问题吗?我可以从Swagger中看到一些创建的源吗? 如果我可以在上面登录,以了解问题的根源?
答案 0 :(得分:0)
如果您使用的是Spring Security,则必须添加一些其他配置,如下所示。
SecurityConfig.java
package com.swagger.demo.security;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer{
//Swagger Resources
@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/");
}
//If you are using Spring Security Add Below Configuration
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**")
.antMatchers(HttpMethod.OPTIONS,"/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/**", "/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**")
.permitAll()
.anyRequest().authenticated();
}
}
SwaggerConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
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
public class SwaggerConfig {
@Bean
public Docket apiDocket() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.swagger.demo"))
.paths(PathSelectors.any())
.build();
return docket;
}
}
pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-core</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
答案 1 :(得分:0)
@Pierrot,能否请您检查应用程序中配置的上下文路径和端口以及URL中用于测试swagger UI的端口和端口是否正确? 正如您提到的那样,当您从头开始创建新项目时,它可以很好地工作,问题可能出在上下文路径/端口上。