Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用Resteasy修饰json响应_Json_Rest_Jackson_Resteasy - Fatal编程技术网

如何用Resteasy修饰json响应

如何用Resteasy修饰json响应,json,rest,jackson,resteasy,Json,Rest,Jackson,Resteasy,我正在用Resteasy实现一个restfull服务,该服务将由Extjs客户端使用,我想用更多的属性来修饰http响应中检索到的json对象,而不必在服务方法中使用带有附加属性的包装类或重写JacksonJsonProvider 例如: 原始对象: { "id":"1", "name":"Diego" } 装饰对象: { "success":"true", "root":{ "id":"1", "name":"Die

我正在用Resteasy实现一个restfull服务,该服务将由Extjs客户端使用,我想用更多的属性来修饰http响应中检索到的json对象,而不必在服务方法中使用带有附加属性的包装类或重写JacksonJsonProvider
例如:

原始对象:

{
   "id":"1",
   "name":"Diego"
}
装饰对象:

{
   "success":"true",
   "root":{
             "id":"1",
             "name":"Diego"
          }
}
我找到了,但无法实现json类型的装饰器

我尝试用包装器替换将被序列化的实体,但如果使用替换集合替换实体,则无效


有什么建议吗?

您可以编写一个拦截器,在将JSON响应传递给客户端之前对其进行包装。下面是一个示例代码:

  • 定义自定义HTTPServletResponseWrapper

    public class MyResponseWrapper extends HttpServletResponseWrapper {
        private ByteArrayOutputStream byteStream;
    
        public MyResponseWrapper(HttpServletResponse response, ByteArrayOutputStream byteStream) {
            super(response);
            this.byteStream = byteStream;
        }
    
        @Override
        public ServletOutputStream getOutputStream() throws IOException {
            return new ServletOutputStream() {
                @Override
                public void write(int b) throws IOException {
                    byteStream.write(b);
                }
            };
        }
        @Override
        public PrintWriter getWriter() throws IOException {
            return new PrintWriter(byteStream);
        }
    }
    
  • 定义过滤器类:

    @WebFilter("/rest/*")
    public class JSONResponseFilter implements Filter {
    
        private final String JSON_RESPONSE = " { \"success\":\"true\", \"root\": ";
        private final String JSON_RESPONSE_CLOSE = "}";
    
        /* .. */
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
    
            // capture result in byteStream by using custom responseWrapper
            final HttpServletResponse httpResponse = (HttpServletResponse) response;
            final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            HttpServletResponseWrapper responseWrapper = new MyResponseWrapper(httpResponse, byteStream);
    
            // do normal processing but capture results in "byteStream"
            chain.doFilter(request, responseWrapper);
    
            // finally, wrap response with custom JSON
          // you can do fancier stuff here, but you get the idea
            out.write(JSON_RESPONSE.getBytes());
            out.write(byteStream.toByteArray());
            out.write(JSON_RESPONSE_CLOSE.getBytes());
        }
    }
    

  • 您可以编写一个拦截器,在将JSON响应传递给客户端之前对其进行包装。下面是一个示例代码:

  • 定义自定义HTTPServletResponseWrapper

    public class MyResponseWrapper extends HttpServletResponseWrapper {
        private ByteArrayOutputStream byteStream;
    
        public MyResponseWrapper(HttpServletResponse response, ByteArrayOutputStream byteStream) {
            super(response);
            this.byteStream = byteStream;
        }
    
        @Override
        public ServletOutputStream getOutputStream() throws IOException {
            return new ServletOutputStream() {
                @Override
                public void write(int b) throws IOException {
                    byteStream.write(b);
                }
            };
        }
        @Override
        public PrintWriter getWriter() throws IOException {
            return new PrintWriter(byteStream);
        }
    }
    
  • 定义过滤器类:

    @WebFilter("/rest/*")
    public class JSONResponseFilter implements Filter {
    
        private final String JSON_RESPONSE = " { \"success\":\"true\", \"root\": ";
        private final String JSON_RESPONSE_CLOSE = "}";
    
        /* .. */
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
    
            // capture result in byteStream by using custom responseWrapper
            final HttpServletResponse httpResponse = (HttpServletResponse) response;
            final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            HttpServletResponseWrapper responseWrapper = new MyResponseWrapper(httpResponse, byteStream);
    
            // do normal processing but capture results in "byteStream"
            chain.doFilter(request, responseWrapper);
    
            // finally, wrap response with custom JSON
          // you can do fancier stuff here, but you get the idea
            out.write(JSON_RESPONSE.getBytes());
            out.write(byteStream.toByteArray());
            out.write(JSON_RESPONSE_CLOSE.getBytes());
        }
    }