为什么我的请求实体InputStream在ContainerRequestFilter(Spring + Jersey)中始终为空

时间:2015-11-21 17:09:43

标签: spring-boot jax-rs jersey-2.0 spring-web

我完全无法在JaxRS ContainerRequestFilter中获取Request有效内容/表单。

我的设置:

  • JDK 8
  • SpringBoot 1.3.0.RELEASE
  • Jersey 2.22.1(来自SpringBoot)

这是我的pom :(来自Spring Initialzr)

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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-jersey</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

这是我的Application类:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

这是我的JerseyConfig:

@Configuration
public class JerseyConfig extends ResourceConfig {
    public JerseyConfig() {
        register(HelloController.class);
        register(MyFilter.class);
    }
}

这是我的HelloController:

@Component
@Path("/hello")
public class HelloController {

    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @POST
    public String onHelloRequest(@FormParam("foo") String foo) {
        System.out.println(foo);
        return foo + "bar";
    }
}

这是我遇到的问题的核心部分,即ContainerRequestFilter:

public class MyFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext ctx) throws IOException {

        System.err.println("I'm in the filter");

        // Solution #1 doesn't work
        if (ctx instanceof ContainerRequest) {
            ContainerRequest request = (ContainerRequest) ctx;

            request.bufferEntity();
            Form f = request.readEntity(Form.class);
            System.err.println(f.asMap().toString());
        }

        // Solution #2 doesn't work either
        InputStream inputStream = ctx.getEntityStream();
        StringBuilder textBuilder = new StringBuilder();
        try (Reader reader = new BufferedReader(new InputStreamReader
                (inputStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
            int c = 0;
            while ((c = reader.read()) != -1) {
                textBuilder.append((char) c);
            }
        }
        System.err.println(textBuilder.toString());
    }
}

正如您所看到的,这是一个使用Jersey的非常精简的SpringBoot示例。但是,看起来已经消耗了ContainerRequestFilter中的InputStream。我用Javax ServletFilter尝试了它,我遇到了同样的问题。

Spring-web是否有可能在调用Jersey之前使用InputStream?

请帮帮我。

修改

为了测试它,我使用了POSTMAN并发送了:

POST http://localhost:8080/hello

接头

Content-type = application / x-www-form-urlencoded

有效载荷:

富=酒吧

我的回复是

BARBAR

我的控制台输出

我在过滤器

{} //解决方案#1&lt; - 应该是类似foo = bar

的东西

//解决方案#2&lt; - 空字符串

EDIT2

即使没有任何自定义过滤器,我也会在控制台中显示该消息:

2015-11-21 20:14:05.438 WARN 4440 --- [nio-8080-exec-2] o.glassfish.jersey.servlet.WebComponent:对URI http://localhost:8080/hello的servlet请求包含表单请求体中的参数但请求体已被servlet或访问请求参数的servlet过滤器使用。只有使用@FormParam的资源方法才能按预期工作。通过其他方式使用请求体的资源方法将无法按预期工作。

在将其传递给Jersey之前,它看起来像其他东西消耗InputStream,让每个Jax-RS过滤器|拦截器都没有状态。

1 个答案:

答案 0 :(得分:1)

这是“解决方案”

在SpringBoot中,如果添加starter-web模块,它将添加spring:webmvc,这似乎与JaxRS和Servlet过滤器不兼容。如果使用SpringBoot和Jersey,请确保webmvc jar不在类路径中。

Spring堆栈将使用Servlet的Request InputStream,并且管道中的每个向下的intercepter / filter都将没有任何内容。

这是一个在Servlet过滤器中获取Request的有效负载的代码片段

public MyFilter implements Filter
{


    @Override
    public final void doFilter(
        final ServletRequest request,
        final ServletResponse response,
        final FilterChain chain)
        throws IOException,
        ServletException {


        getRawFormPayload((HttpServletRequest) request);
        chain.doFilter(request, response);
    }

     protected String getRawFormPayload(HttpServletRequest request) {

        final int contentLength = request.getContentLength();
        final StringBuilder payloadBuilder = new StringBuilder(contentLength);
        try {
            final InputStream inputStream = request.getInputStream();
            final BufferedReader reader =
                new BufferedReader(new InputStreamReader(inputStream, request.getCharacterEncoding()));

            // Buffer should not be higher than the content-length of the request
            reader.mark(contentLength + 1);
            final char[] charBuffer = new char[contentLength + 1];
            int bytesRead = -1;

            while ((bytesRead = reader.read(charBuffer)) > 0) {
                payloadBuilder.append(charBuffer, 0, bytesRead);
            }

            // Reset the buffer so the next Filter/Interceptor have unaltered InputStream
            reader.reset();

        } catch (final IOException e) {
            this.LOG.error(e.getMessage(), e);
        }
        return payloadBuilder.toString();
    }
}