有什么方法可以让带有 Jersey 的 Spring Boot 提供静态内容吗?我已经阅读了一系列关于将 Swagger 集成到 Spring Boot 应用程序中的教程和代码示例。我可以让它提供基本的 swagger.json,但我无法让 Swagger UI 工作。
我什至不能让它传送一个简单的 hello.txt
静态文件。
我的 pom.xml 的相关部分是:
<!--Spring Boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Jersey -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>1.5.7</version>
</dependency>
还有我的代码:
@Configuration
@EnableAutoConfiguration
@ComponentScan({"com.xxxx"})
public class AdminApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(AdminApplication.class)
.run(args);
}
@Bean
public ServletRegistrationBean jerseyServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/*");
registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());
return registration;
}
}
package com.xxxxxx.admin.config;
import com.xxxxxx.admin.resource.Status;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;
import io.swagger.jaxrs.config.BeanConfig;
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(RequestContextFilter.class);
packages("com"); // TODO needs more detailed level
register(LoggingFilter.class);
// Validation
this.property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
this.property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, true);
configureSwagger();
}
private void configureSwagger() {
register(io.swagger.jaxrs.listing.ApiListingResource.class);
register(io.swagger.jaxrs.listing.SwaggerSerializers.class);
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.0");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:8080");
beanConfig.setBasePath("/"); // tried other things like "/api", but doesn't change anything
beanConfig.setResourcePackage("com.xxxxxx.admin");
beanConfig.setPrettyPrint(true);
beanConfig.setScan(true);
}
}
//other imports
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Service
@Path("/status")
@Api(value = "status", description = "Check status")
public class Status {
@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Return status")
public Response status() {
return Response.ok("Up").build();
}
}
我还尝试让 Jersey 作为过滤器运行(使用 spring.jersey.type=filter
)并更改 Jersey 的 servlet 模式,如 this answer 中所述,但这似乎没有任何影响。
@ApplicationPath("/rootPath")
public class JerseyConfig extends ResourceConfig {
我在/src/main/resources/public 下有一个 hello.txt 文件,在/src/main/resources/public/swagger 下有 Swagger UI 的静态文件。
正如我所说,我的应用程序运行良好,并且 GET http://localhost:8080/swagger.json向我展示了普通的 json 文档,但两者都是 http://localhost:8080/hello.txt和 http://localhost:8080/swagger/index.html返回 404。
我正在使用 Jersey 2.8 和 Spring Boot 1.3.0
最佳答案
I also tried changing Jersey's servlet pattern
@ApplicationPath("/rootPath") public class JerseyConfig extends ResourceConfig {
您配置应用程序的方式,@ApplicationPath
没关系。它适用于 this answer you linked to 的原因,是因为 Spring Boot 自动配置在提取 @ApplicationPath
时设置了 servlet 映射来自您的资源配置的值。
您目前没有使用 ServletRegistrationBean
由 Spring Boot 提供,可以实现这一点。如果您有目标,请使用您自己的 ServletRegistrationBean
, 以便您可以注册您的 ResourceConfig
,您可以通过以下任一方式完成相同的操作
- 注释您的
ResourceConfig
与@Component
使它成为一个 Spring bean,或者 在你的配置类中让它成为一个Spring bean
@Bean public ResourceConfig config() { return new JerseyConfig(); }
然后 Spring Boot 将注入(inject)您的 ResourceConfig
进入JerseyAutoConfiguration
, 它会得到 @ApplicationPath
ResourceConfig
上的值(如果存在) , 并用它来注册自己的 ServletRegistrationBean
.
你可以看到 JerseyAutoConfiguration
当您让 Spring Boot 处理配置时,了解您免费获得的一切。
如果您想保留当前的 SpringRegistrationBean
,只需更改您正在使用的路径即可。您正在使用 /*
,这是链接答案中提到的问题。所以只需更改为 /rooPath/*
如果这就是您想要的。
关于java - 无法使用 Spring Boot 和 Jersey 2 提供静态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35342581/