英文:
Swagger UI is not rendering on the browser
问题
我已经将 Swagger 依赖项添加到 Spring Boot 应用程序中,并且 JSON 正在按预期加载。当我尝试通过调用此 URL http://localhost:9090/swagger-ui.html
来加载 UI 时,在浏览器上显示以下错误。
Whitelabel Error Page
此应用程序没有针对 /error 的显式映射,因此您正在看到此作为回退。
发生了意外错误(类型=Not Found,状态=404)。
pom.xml
<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>
</dependency>
config class
@Configuration
@EnableSwagger2
public class SwaggerConfig {
}
附注:我的应用程序在端口 9090 上运行。
英文:
I have added swagger dependencies to the spring boot application and JSON is loading as expected. When I try to load UI by calling this URL http://localhost:9090/swagger-ui.html
then the following error is displayed on the browser.
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Not Found, status=404).
pom.xml
<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>
</dependency>
config class
@Configuration
@EnableSwagger2
public class SwaggerConfig {
}
Ps - My application is running under the port 9090
答案1
得分: 4
我找到了解决方案。从 Swagger 3.0 开始,我们不需要将两个依赖项添加到构建工具中。springfox-boot-starter
可以替代这两个依赖项。
pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
从 Swagger 3.0 开始,URL 应该是 http://localhost:9090/swagger-ui/
,而不是 http://localhost:9090/swagger-ui.html
。
英文:
I found the solution. From Swagger 3.0 we don't need to add 2 dependencies into the build tool. springfox-boot-starter
can be replaced instead of those 2 dependencies.
pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Form swagger 3.0 the URL should be http://localhost:9090/swagger-ui/
rather than http://localhost:9090/swagger-ui.html
答案2
得分: 1
我遇到过类似的问题。确保你声明了 Swagger 项目。
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//Swagger UI 属性
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
这必须在实现的配置中用 "WebMvcConfigurer" 来声明。
更多信息请查看:https://springfox.github.io/springfox/docs/current/
英文:
i had similar issue. Make sure you declerate the swagger items.
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//Swagger UI property
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
This must be declareted in the config of implementation with "WebMvcConfigurer".
More information here : https://springfox.github.io/springfox/docs/current/
答案3
得分: 0
如果您定义了基本URL,例如 /rest
、/api
,则您还需要将其添加进去。
我的配置如下所示:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurerAdapter{
@Bean
public Docket APIs() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(metadata())
.select()
.apis(RequestHandlerSelectors.basePackage("<rest_controller_package>"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo metadata() {
return new ApiInfoBuilder()
.title("<title_here>")
.description("<description_here>")
.version("BETA")
.build();
}
}
还有 pom.xml 文件:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
我通过 http://localhost:9090/api/swagger-ui.html
进行访问。
英文:
If you have base url defined, for eg /rest
, /api
then you will need to put that as well.
My config looks like this:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurerAdapter{
@Bean
public Docket APIs() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(metadata())
.select()
.apis(RequestHandlerSelectors.basePackage("<rest_controller_package>"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo metadata() {
return new ApiInfoBuilder()
.title("<title_here>")
.description("<description_here>")
.version("BETA")
.build();
}
}
and pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
and I access it via http://localhost:9090/api/swagger-ui.html
答案4
得分: 0
我认为你应该在 SwaggerConfig 类中添加一个返回 Docket 对象的 Bean,其中指定你想在 Swagger 中显示的路径和包。类似于这样:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.spring.rest.controller"))
.paths(PathSelectors.any())
.build();
}
}
英文:
I think you should add into the SwaggerConfig class a Bean that return a Docket object, which specify the paths and the packages that you want to show in swagger. Something like this:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage(“com.spring.rest.controller”))
.paths(PathSelectors.any())
.build();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论