java – 在过滤器中修改Jersey InputStream.无法弄清楚如何在Jersey资源中访问修改后的inputStream

栏目: Java · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/32632342/jersey-inputstream-is-modified-in-filter-unable-to-figure-out-how-to-access-mod
正如 How to use Jersey interceptors to get request body

中所讨论的,我正在修改ContainerRequestFilter中的EntityInputStream.

public filter(ContainerRequest request){
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     InputStream in = request.getEntityInputStream(); 
     try{
           Readerwriter.writeTo(in, out);
           byte[] requestEntity = out.toByteArray();

           // DO SOMETHING WITH BYTES HERE

           request.setEntityInputStream(new ByteArrayInputStream(requestEntity));

      }/// error handling code here  
}

但是,后来我无法弄清楚如何访问修改后的InputStream.我可以在资源中获取ServletContext,但我无法弄清楚如何获取我在过滤器中实际修改的对象ContainerRequest.

我可以这样做吗?当我尝试这个时,泽西岛无法启动:

@Post
@Path("/test")
public Response test(@Context ContainerRequest cr){
     // blah blah
     return....
}

泽西岛错误:

方法public javax.ws.rs.core.Response example.TestController.test(com.sun.jersey.spi.container.ContainerRequest)缺少依赖性,用资源的POST注释,类example.TestController,不被识别为有效资源方法.

我被困在一个旧版本的球衣,1.8,所以我不确定这是否是问题的一部分.

您需要做的就是接受一个I​​nputStream作为资源方法中的实体主体.如果你想要ByteArrayInputStream只是强制转换它.

@POST
public Response post(InputStream in) {
    ByteArrayInputStream bin = (ByteArrayInputStream)in;
}

如果你还不知道,Jersey如何将请求流(对于请求体)转换为 Java 类型(例如JSON到POJO)的时间是 MessageBodyReader s.您可以在 JAX-RS Entity Providers 阅读更多相关信息.

Jersey已经附带了一些易于转换类型的标准阅读器,例如String.大多数内容类型都可以转换为String.同样,它有一个处理InputStream的阅读器.这可能是最简单的转换,因为请求已经作为InputStream进入,所以读者真正需要做的就是返回原始流,这就是传递给我们方法的内容.

如果我们查看实现 InputStreamProvider ,我们可以看到实际发生的情况. 0700 3.由于过滤器发生在读者之前,读者只需返回我们设置的流.

这是一个使用 Jersey Test Framework 的完整示例

public class StreamFilterTest extends JerseyTest {

    public static class InputStreamFilter implements ContainerRequestFilter {

        @Override
        public ContainerRequest filter(ContainerRequest request) {
            try {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                InputStream in = request.getEntityInputStream();
                ReaderWriter.writeTo(in, out);

                byte[] requestBytes = out.toByteArray();
                byte[] worldBytes = " World".getBytes(StandardCharsets.UTF_8);
                byte[] newBytes = new byte[requestBytes.length + worldBytes.length];
                System.arraycopy(requestBytes, 0, newBytes, 0, requestBytes.length);
                System.arraycopy(worldBytes, 0, newBytes, requestBytes.length, worldBytes.length);

                request.setEntityInputStream(new ByteArrayInputStream(newBytes));
            } catch (IOException ex) {
                Logger.getLogger(InputStreamFilter.class.getName()).log(Level.SEVERE, null, ex);
                throw new RuntimeException(ex);
            }

            return request;
        }
    }

    @Path("stream")
    public static class StreamResource {

        @POST
        public String post(InputStream in) throws Exception {
            ByteArrayInputStream bin = (ByteArrayInputStream) in;
            StringWriter writer = new StringWriter();
            ReaderWriter.writeTo(new InputStreamReader(bin), writer);
            return writer.toString();
        }
    }

    public static class AppConfig extends DefaultResourceConfig {
        public AppConfig() {
            super(StreamResource.class);
            getContainerRequestFilters().add(new InputStreamFilter());
        }
    }

    @Override
    public WebAppDescriptor configure() {
        return new WebAppDescriptor.Builder()
                .initParam(WebComponent.RESOURCE_CONFIG_CLASS,
                        AppConfig.class.getName())
                .build();
    }

    @Test
    public void should_return_hello_world() {
        String response = resource().path("stream").post(String.class, "Hello");
        assertEquals("Hello World", response);
    }
}

这是测试依赖

<dependency>
    <groupId>com.sun.jersey.jersey-test-framework</groupId>
    <artifactId>jersey-test-framework-grizzly2</artifactId>
    <version>1.17.1</version>
    <scope>test</scope>
</dependency>

翻译自:https://stackoverflow.com/questions/32632342/jersey-inputstream-is-modified-in-filter-unable-to-figure-out-how-to-access-mod


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Go Web 编程

Go Web 编程

[新加坡]Sau Sheong Chang(郑兆雄) / 黄健宏 / 人民邮电出版社 / 2017-11-22 / 79

《Go Web 编程》原名《Go Web Programming》,原书由新加坡开发者郑兆雄(Sau Sheong Chang)创作、 Manning 出版社出版,人名邮电出版社引进了该书的中文版权,并将其交由黄健宏进行翻译。 《Go Web 编程》一书围绕一个网络论坛 作为例子,教授读者如何使用请求处理器、多路复用器、模板引擎、存储系统等核心组件去构建一个 Go Web 应用,然后在该应用......一起来看看 《Go Web 编程》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

html转js在线工具
html转js在线工具

html转js在线工具