有没有办法让Spring Boot与Jersey一起提供静态内容?我已经查看了一系列关于将Swagger集成到Spring Boot应用程序中的教程和代码示例。我可以使其传递基本的swagger.json,但我无法使Swagger UI工作。
我甚至不能传递一个简单的hello.txt静态文件。
我的pom.xml的相关部分为:
我还尝试将Jersey作为过滤器运行(使用
我有一个hello.txt文件在/src/main/resources/public下,Swagger UI的静态文件在/src/main/resources/public/swagger下。
正如我所说,我的应用程序运行良好,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。
我甚至不能传递一个简单的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
),并按this answer中所述更改Jersey的servlet模式,但似乎没有影响任何内容。@ApplicationPath("/rootPath")
public class JerseyConfig extends ResourceConfig {
我有一个hello.txt文件在/src/main/resources/public下,Swagger UI的静态文件在/src/main/resources/public/swagger下。
正如我所说,我的应用程序运行良好,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。
spring.jersey.type=filter
和spring.jersey.application-path=rootPath
,一切都很好:静态内容被服务并且应用程序端点被移动到rootPath/
并正确响应。但是,如果我只设置spring.jersey.type=filter
而没有设置spring.jersey.application-path
,那么静态内容将无法访问。根据您链接的答案中的选项1,将jersey设置为运行过滤器就足够了。我错过了什么? - user384729ExceptionMapper<NotFoundException>.toResponse
中删除return Response.status(Response.Status.NOT_FOUND).entity(exception.getMessage()).build()
的entity()
部分。再次感谢。 - user384729