首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不能使用Spring和泽西2提供静态内容

不能使用Spring和泽西2提供静态内容
EN

Stack Overflow用户
提问于 2016-02-11 14:52:42
回答 2查看 1.9K关注 0票数 3

有没有办法让Spring与泽西岛提供静态内容?我已经学习了一系列关于将Swagger集成到Spring应用程序的教程和代码示例。我可以让它交付基本的swagger.json,但我不能使Swagger工作。

我甚至不能让它提供一个简单的hello.txt静态文件。

我的pom.xml的相关部分是:

代码语言:javascript
运行
复制
<!--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>

我的密码是:

代码语言:javascript
运行
复制
@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();
    }
}

我还尝试让泽西岛作为过滤器运行(使用spring.jersey.type=filter),并如this answer中所述改变泽西的servlet模式,但这似乎没有影响任何事情。

代码语言:javascript
运行
复制
@ApplicationPath("/rootPath")
public class JerseyConfig extends ResourceConfig {

我在/src/main/resources/public下有一个hello.txt文件,在/src/main/resources/public/swagger下有一个Swagger的静态文件。

如前所述,我的应用程序运行良好,GET http://localhost:8080/swagger.json向我展示了普通的json文档,但http://localhost:8080/hello.txthttp://localhost:8080/swagger/index.html都返回404。

我用的是泽西岛2.8和Spring 1.3.0

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-11 15:26:58

我还试着改变泽西的servlet模式 @ApplicationPath("/rootPath")公共类JerseyConfig扩展ResourceConfig {

您配置应用程序的方式,@ApplicationPath并不重要。它适用于this answer you linked to的原因是Spring自动配置在从资源配置中提取@ApplicationPath值时设置servlet映射。

您目前没有使用Spring提供的ServletRegistrationBean,它实现了这一点。如果您的目标是使用您自己的ServletRegistrationBean,以便您可以注册您的ResourceConfig,那么您也可以通过以下两种方法进行相同的操作

  1. ResourceConfig注释@Component,使其成为Spring,或者
  2. 在您的配置类中使它成为Spring @Bean公共ResourceConfig config() {返回新的JerseyConfig();}

Spring然后将您的ResourceConfig注入到JerseyAutoConfiguration中,在那里它将获得ResourceConfig上的@ApplicationPath值(如果存在的话),并使用它注册自己的ServletRegistrationBean

当您让Spring处理配置时,您可以看到JerseyAutoConfiguration来获得免费的所有信息。

如果希望保留当前的SpringRegistrationBean,只需更改所使用的路径即可。您正在使用/*,这是链接答案中提到的问题。所以,如果您想要的话,只需更改为/rooPath/*即可。

票数 2
EN

Stack Overflow用户

发布于 2016-02-11 15:35:56

在使用Spring时,它看起来是一个常见的问题。每个servlet规范都需要一个servlet容器来实现优先级最低的默认服务器,该服务器能够提供位于WEB文件夹之外的静态内容。不幸的是,您正在将Jersey映射到"/*",这意味着每个URL都将提交给泽西,而泽西不知道如何处理静态URL。

那么,什么(容易)可以做呢?

  • 将Jersey映射到子路径(例如/api),并将所有控制器移动到那里: ServletRegistrationBean注册=新ServletRegistrationBean(新ServletContainer(),"/api/*");.BeanConfig.setBasePath(“/api/”) 问GET http://localhost:8080/api/swagger.json
  • 只将servlet映射到*.json URL: ServletRegistrationBean注册=新ServletRegistrationBean(新ServletContainer(),"*.json");
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35342581

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档