无法访问 SwaggerUI index.html 页面,但可以打开 api-docs(Spring - 启动)
Cannot access SwaggerUI index.html page, but can open api-docs (Spring - Boot)
我正在尝试一个小应用程序来学习 spring 大摇大摆地启动 UI。当我 运行 程序时,我可以打开 http://localhost:8080/api-docs
显示 json 输出。但是当我访问 http://localhost:8080/swagger/index.html
时,它给出了 Whitelabel 错误页面。
我遵循了哪些步骤:
1- 从 GitHub 中可用的 swagger-ui 项目复制了 "dist" 文件夹。
2- 将 "dist" 重命名为 "swagger" 并将其放入 src/main/public
3- 将 index.html 中的路径更改为“/api-docs”。
下面是EclipseLuna的截图IDE和所有的程序代码,
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rajkishan.learnSpring</groupId>
<artifactId>RestfulWithSpring</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>LearnRESTfulSpring</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.12.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<start-class>com.rajkishan.Application</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>0.8.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Application.java
package com.rajkishan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
* Spring Boot Starter Class
*
*/
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableWebMvc
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Greeting.java
是 spring-restful 中的普通 pojo-https://spring.io/guides/gs/rest-service/
中提供的演示
GreetingController.java
package com.rajkishan;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping(value = "/greeting", method = RequestMethod.GET, produces = "application/json")
public Greeting greeting(@RequestParam (value = "name", defaultValue = "World") String name){
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
}
SwaggerConfig.java
package com.rajkishan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
import com.wordnik.swagger.model.ApiInfo;
@Configuration
@EnableSwagger
public class SwaggerConfig {
private SpringSwaggerConfig swaggerConfig;
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig swaggerConfig){
this.swaggerConfig = swaggerConfig;
}
@Bean
public SwaggerSpringMvcPlugin customSwaggerPlugin(){
return new SwaggerSpringMvcPlugin(this.swaggerConfig).apiInfo(apiInfo())
.includePatterns("/greeting/.*");
}
private ApiInfo apiInfo(){
ApiInfo apiInfo = new ApiInfo("My Spring Application",
"Learning Spring Restful", "termsOfServiceUrl", "contact", "license", "licenseUrl");
return apiInfo;
}
}
Index.html
$(function() {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "/api-docs"
}
/*Rest of the Code Follows...*/
你能看出我哪里做错了吗?
也许我错过了什么?
在你的 Application
class 中删除所有注释并将其替换为 spring-boot 1.2 及更高版本中的 @SpringBootApplication
和 @EnableAutoConfiguration
的组合@ComponentScan
低于 1.2.x 的引导版本。特别是删除 @EnableWebMvc
注释,因为它会干扰资源的 spring 启动自动配置,即提供 swagger-ui 的那个。
此外,您的 swagger-springmvc 依赖项非常过时,您应该至少使用 1.0.2。在相关说明中,您是否考虑过使用 springfox 移动?它是下一代 swagger-springmvc 并且它还支持 swagger 2.0。
我正在尝试一个小应用程序来学习 spring 大摇大摆地启动 UI。当我 运行 程序时,我可以打开 http://localhost:8080/api-docs
显示 json 输出。但是当我访问 http://localhost:8080/swagger/index.html
时,它给出了 Whitelabel 错误页面。
我遵循了哪些步骤:
1- 从 GitHub 中可用的 swagger-ui 项目复制了 "dist" 文件夹。
2- 将 "dist" 重命名为 "swagger" 并将其放入 src/main/public
3- 将 index.html 中的路径更改为“/api-docs”。
下面是EclipseLuna的截图IDE和所有的程序代码,
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rajkishan.learnSpring</groupId>
<artifactId>RestfulWithSpring</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>LearnRESTfulSpring</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.12.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<start-class>com.rajkishan.Application</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>0.8.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Application.java
package com.rajkishan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
* Spring Boot Starter Class
*
*/
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableWebMvc
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Greeting.java
是 spring-restful 中的普通 pojo-https://spring.io/guides/gs/rest-service/
GreetingController.java
package com.rajkishan;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping(value = "/greeting", method = RequestMethod.GET, produces = "application/json")
public Greeting greeting(@RequestParam (value = "name", defaultValue = "World") String name){
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
}
SwaggerConfig.java
package com.rajkishan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
import com.wordnik.swagger.model.ApiInfo;
@Configuration
@EnableSwagger
public class SwaggerConfig {
private SpringSwaggerConfig swaggerConfig;
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig swaggerConfig){
this.swaggerConfig = swaggerConfig;
}
@Bean
public SwaggerSpringMvcPlugin customSwaggerPlugin(){
return new SwaggerSpringMvcPlugin(this.swaggerConfig).apiInfo(apiInfo())
.includePatterns("/greeting/.*");
}
private ApiInfo apiInfo(){
ApiInfo apiInfo = new ApiInfo("My Spring Application",
"Learning Spring Restful", "termsOfServiceUrl", "contact", "license", "licenseUrl");
return apiInfo;
}
}
Index.html
$(function() {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "/api-docs"
}
/*Rest of the Code Follows...*/
你能看出我哪里做错了吗? 也许我错过了什么?
在你的 Application
class 中删除所有注释并将其替换为 spring-boot 1.2 及更高版本中的 @SpringBootApplication
和 @EnableAutoConfiguration
的组合@ComponentScan
低于 1.2.x 的引导版本。特别是删除 @EnableWebMvc
注释,因为它会干扰资源的 spring 启动自动配置,即提供 swagger-ui 的那个。
此外,您的 swagger-springmvc 依赖项非常过时,您应该至少使用 1.0.2。在相关说明中,您是否考虑过使用 springfox 移动?它是下一代 swagger-springmvc 并且它还支持 swagger 2.0。