如何解决Swagger UI-无法推断Swagger 2.9.2中的基本URL
因此,两天前我才刚开始使用Swagger,我观看了一个视频,该视频介绍了如何在我的API上配置基本的Swagger文档。视频只是告诉您使用以下代码创建一个新类:
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("your-api-package")
.paths(PathSelectors.any())
.build();
}
我下载了spring-instrument.jar并将其添加到我的项目中。在pom.xml中添加了相关性,仅此而已。然后,当我尝试访问localhost / my-project / swagger-ui.html时,出现了无法推断基本URL错误。在网上查找时,发现我也需要添加Swagger Security,因此最终下载了spring-security.jar。我使用以下代码创建了SwaggerSecurity类:
private static final String[] AUTH_LIST = {
"/swagger-resources/**","/swagger-ui.html","/v2/api-docs","/webjars/**"
};
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().antMatchers(AUTH_LIST).authenticated()
.and()
.httpBasic().authenticationEntryPoint(swaggerAuthenticationEntryPoint())
.and()
.csrf().disable();
}
@Bean
public BasicAuthenticationEntryPoint swaggerAuthenticationEntryPoint() {
BasicAuthenticationEntryPoint entryPoint = new BasicAuthenticationEntryPoint();
entryPoint.setRealmName("Swagger Realm");
return entryPoint;
}
哪个似乎可以解决某些人的问题,但错误仍然存在。因此,我想知道是否有人也经历过这种情况,以及你们如何解决了这个问题。我不知道它是否会增加任何问题,但是我正在使用最新的Eclipse版本来运行该项目。
解决方法
很多事情在您的问题中尚不清楚,但让我尝试。
如果您正在运行带有嵌入式tomcat的spring app,则需要定义servlet上下文路径。只需在您的src/main/resources/application.properties
server.servlet.context-path=/my-project
然后尝试再次运行,应该可以。
如果仍然无法使用,请尝试将Docket
bean的定义修改为:
@Autowired
private javax.servlet.ServletContext servletContext;
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.pathProvider(new springfox.documentation.spring.web.paths.RelativePathProvider(servletContext) {
@Override
protected String applicationPath() {
return "/my-project";
}
})
.select()
.apis(RequestHandlerSelectors.basePackage("your-api-package"))
.paths(PathSelectors.any())
.build();
}
,
添加以下依赖项:
<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>
在项目中扩展WebMvcConfigurationSupport
,并覆盖 addResourceHandlers 。
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("your-base-package"))
.paths(PathSelectors.any())
.build();
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
在控制器中注释@Api
和@ApiOperation
。
@RestController
@Api(tags = {"Hello controller"})
@RequestMapping(value = "api")
public class HelloController {
@ApiOperation(value = "greet")
@GetMapping(value = "/greet",consumes = "application/json")
public ResponseEntity<String> index() {
return ResponseEntity.ok("Greetings from Spring Boot!");
}
}
这就像一种魅力。启动您的服务。在浏览器中点击localhost:8080/swagger-ui.html。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。