我正在将基于springMVC的项目迁移到spring boot。这是一个多模块项目。我无法启动swagger-ui.html。我添加了调度程序servlet,如下所示:
@Configuration
public class DispatcherServletConfig {
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
ServletRegistrationBean registration = new
ServletRegistrationBean(dispatcherServlet(), "/prefix1/*");
registration.addUrlMappings("/admin/*");
registration.addUrlMappings("/prefix2/*");
registration.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);
return registration;
}
}
private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES =
new HashSet<String>(Arrays.asList("application/json",
"application/xml"));
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES =
new HashSet<String>(Arrays.asList("application/json",
"application/xml"));
@Bean
public Docket api() {
ParameterBuilder aParameterBuilder = new ParameterBuilder();
aParameterBuilder.name("Authorization").modelRef(new ModelRef("string")).parameterType("header")
.description("Authorization Header").required(true).build();
List<Parameter> aParameters = new ArrayList<>();
aParameters.add((Parameter) aParameterBuilder.build());
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build().apiInfo(apiInfo()).globalOperationParameters(aParameters).produces(DEFAULT_PRODUCES_AND_CONSUMES);
}
private ApiInfo apiInfo() {
Contact contact = new Contact("Power2SME Pvt. Ltd.", "http://www.power2sme.com", "support@power2sme.com");
return new ApiInfo("NBFC API", "Information related to api exposed by NBFC system.", "1.0",
"https://www.power2sme.com/termsandconditions", contact, "License of API",
"https://www.power2sme.com/privacypolicy", new ArrayList<>());
}
}
我在pom.xml中添加了以下依赖项:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.0.6</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.2-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19.1</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scribe</groupId>
<artifactId>scribe</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>3.10.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>3.10.0</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-jackson</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
也是一个插件:
<plugin>
<groupId>io.github.robwin</groupId>
<artifactId>swagger2markup-maven-plugin</artifactId>
<version>0.9.3</version>
<configuration>
<inputDirectory>${project.basedir}/apiDoc/json</inputDirectory>
<swaggerFile>swagger.json</swaggerFile>
<outputDirectory>${project.basedir}/apiDoc/asciidoc</outputDirectory>
<markupLanguage>asciidoc</markupLanguage>
</configuration>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>process-swagger</goal>
</goals>
</execution>
</executions>
</plugin>
我已将apiDocs存储在我的项目文件夹下。
每当我致电{project_name} /prefix2/swagger-ui.html#
时,我都会收到404错误每当我调用{project_name} /swagger-ui.html# url时,我都会得到这个
我该如何解决这个问题? 谢谢
答案 0 :(得分:10)
对于使用较新版本 swagger 的用户,这里是访问 swagger ui 页面的解决方案。
替换这个:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
这样:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
在您的 pom.xml 文件中。
通过以下网址访问页面(避免使用 .html 扩展名):http://localhost:8080/swagger-ui/
重建并重启服务器。你的问题就解决了!
答案 1 :(得分:4)
在我的情况下,降低摇摇欲坠的版本可以解决问题。
看不到swagger ui(404)
<swagger.version>3.0.0</swagger.version>
工作正常:
<swagger.version>2.9.2</swagger.version>
依赖项:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
答案 2 :(得分:3)
尝试使用
swagger-ui/index.html
代替
swagger-ui.html
答案 3 :(得分:0)
在spring boot项目中,您必须执行以下操作:
Swagger的配置主要围绕Docket bean。
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("org.example.sp.controller"))
.paths(PathSelectors.ant("/foos/*"))
.build();
}
Swagger 2通过@EnableSwagger2
注释启用。
要验证Springfox是否正常运行,可以在浏览器中访问以下URL:
http://servert:port /project-name/api/v2/api-docs
结果是带有大量键值对的JSON响应,这不是很容易理解的。幸运的是,Swagger为此提供了Swagger UI。
要检查您的swagger-ui,可以通过url来完成:http://server:8080/toto/swagger-ui.html
通过这种方式,我们必须通过一些大胆的注释来注释端点: 有一个简单的文档here
或者您可以按照示例here
答案 4 :(得分:0)
尝试在以下配置文件中添加以下代码:
@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/");
}*
答案 5 :(得分:0)
我认为webMvcConfigurer存在问题。在这种特殊情况下,您的SwaggerConfig Bean仅用@Configuration
和@EnableSwagger2
进行注释。这两个注释足以读取配置并在url上运行Springfox:
http://servert:port /project-name/api/v2/api-docs
但是,如果要公开网页,则需要通过在配置Bean上添加@EnableWebMvc
注释来启用webMvc,并为webMvcConfigurer提供适当的资源处理程序。
我在下面发布的任何其他配置代码都足以暴露Swagger UI:
@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
@Bean
public WebMvcConfigurer webMvcConfigurer()
{
return new WebMvcConfigurer()
{
@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/" );
}
};
}
}
答案 6 :(得分:0)
(声誉使我无法评论答案)
就我而言,降级招摇也能解决问题
发件人:
<swagger.version>3.0.0</swagger.version>
收件人
<swagger.version>2.9.2</swagger.version>
答案 7 :(得分:0)
检查swagger-ui的jar版本。添加
@Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2)
.select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any())
.build(); }
和pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
带有网址:http:// localhost:8080 / swagger-ui.html
这对我来说很好。如果您使用其他版本的springfox-swagger-ui jar,只需打开该jar并检查swagger-ui.html的路径并相应地更改URL。