如何在RESTEasy中启用JSONP?

时间:2011-03-18 10:52:51

标签: java json rest jsonp resteasy

标题说我的问题。我需要将DTO包装到javascript方法回调中。目前我根据要求返回JSON。但是在Ajax中使用它的问题是因为我将GET发送到其他域。当然还有安全警察。

我有创意添加提供。你有任何例子,链接或建议如何做到这一点。

5 个答案:

答案 0 :(得分:12)

RESTEasy中没有对JSONP的明确支持,但是在应用程序中启用JSONP的一种简单方法是编写Servlet过滤器。

以下是一些可以帮助您编写过滤器的链接:

当我有这个要求时,我最终写了自己的,因为我找到的所有例子似乎都没有说明。以下是我编写自己的过滤器的建议:

  • 仅在指定了回调参数(显然)

  • 时才包装响应
  • 仅在响应内容类型为application/json时才包装响应(或者如果您想支持更广泛的变体选择,只有在响应内容类型为application/json或{{时才进行换行1}})

  • 使用HttpServletResponseWrapper,以便您可以调用前向链(application/*+json)而无需将任何数据写入实际响应。完成前向链后,您可以检查内容类型,确保要将响应包装为JSONP,然后将捕获的数据与JSONP前缀和后缀一起写入真实响应。

  • 当您决定将响应包装为JSONP时,请确保将响应内容类型更改为chain.doFilter

如果您之前没有对Java EE过滤器做过多少工作,您可能需要先阅读Java EE教程的相关部分:Filtering Requests and Responses

答案 1 :(得分:4)

我为此问题制定了草稿解决方法。试试吧。此解决方案通过http get参数获取数据并转换为虚拟POST请求。

<强> JQuery的:

function call(){
var val = '{"routes":[{"arrivalAddress":{"fullAddress":"DME"},"destinationAddress":{"fullAddress":"SVO"}}],"carsCount":"1"}';
var jHandler = "doMap";
$.getJSON("http://xxx:yyy/app-0.0.0.1/rest/requestPrice?callback=" + jHandler + "&json=" + encodeURIComponent(val)+"&jsoncallback=?", null, null, "json");
}

function doMap(obj){
alert(obj);
}

服务界面中的声明

@POST
@Path("requestPrice")
@Produces("application/json")
@Consumes("application/json")
PriceResponse requestPrice(PriceRequest request) throws ServiceException;

过滤类:

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class JSONPRequestFilter implements Filter {
    private String callbackParameter;

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        if (!(request instanceof HttpServletRequest)) {
            throw new ServletException("This filter can " +
                    " only process HttpServletRequest requests");
        }

        final HttpServletRequest httpRequest = (HttpServletRequest) request;
        final HttpServletResponse httpResponse = (HttpServletResponse) response;

        if (isJSONPRequest(httpRequest)) {
            RequestWrapper requestWrapper = new RequestWrapper(httpRequest);
            requestWrapper.setContentType("application/json; charset=UTF-8");
            requestWrapper.setHeader("cache-control", "no-cache");
            requestWrapper.setHeader("accept", "application/json");
            requestWrapper.setCharacterEncoding("UTF-8");
            requestWrapper.setBody(httpRequest.getParameter("json"));
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(httpResponse) {

                @Override
                public ServletOutputStream getOutputStream() throws IOException {
                    return new ServletOutputStream() {
                        @Override
                        public void write(int b) throws IOException {
                            baos.write(b);
                        }
                    };
                }

                @Override
                public PrintWriter getWriter() throws IOException {
                    return new PrintWriter(baos);
                }

                public String getData() {
                    return baos.toString();
                }
            };

            chain.doFilter(requestWrapper, responseWrapper);
            response.getOutputStream().write((getCallbackParameter(httpRequest) + "(").getBytes());
            response.getOutputStream().write(baos.toByteArray());
            response.getOutputStream().write(");".getBytes());

            response.setContentType("text/javascript");
        } else {
            chain.doFilter(request, response);
        }
    }

    private String getCallbackMethod(HttpServletRequest httpRequest) {
        return httpRequest.getParameter(callbackParameter);
    }

    private boolean isJSONPRequest(HttpServletRequest httpRequest) {
        String callbackMethod = getCallbackMethod(httpRequest);
        return (callbackMethod != null && callbackMethod.length() > 0);
    }

    private String getCallbackParameter(HttpServletRequest request) {
        return request.getParameter(callbackParameter);
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        callbackParameter = filterConfig.getInitParameter("callbackParameter");
    }

    public void destroy() {
    }

    void printRequest(HttpServletRequest request) throws IOException {
        {
            System.out.println("--------------Headers---------------");
            Enumeration en = request.getHeaderNames();
            while (en.hasMoreElements()) {
                String val = en.nextElement().toString();
                System.out.println(val + " :");
                Enumeration en1 = request.getHeaders(val);
                while (en1.hasMoreElements()) {
                    System.out.println("\t" + en1.nextElement());
                }
            }
        }
        {
            System.out.println("------------Parameters--------------");
            Enumeration en = request.getParameterNames();
            while (en.hasMoreElements()) {
                String val = en.nextElement().toString();
                System.out.println(val + " :");
                String[] en1 = request.getParameterValues(val);
                for (String val1 : en1) {
                    System.out.println("\t" + val1);
                }
            }
        }
        System.out.println("---------------BODY--------------");
        BufferedReader is = request.getReader();
        String line;
        while ((line = is.readLine()) != null) {
            System.out.println(line);
        }
        System.out.println("---------------------------------");

        System.out.println("ContentType: " + request.getContentType());
        System.out.println("ContentLength: " + request.getContentLength());
        System.out.println("characterEncodings: " + request.getCharacterEncoding());
        System.out.println("AuthType: " + request.getAuthType());

        System.out.println("ContextPath: " + request.getContextPath());
        System.out.println("Method: " + request.getMethod());

    }

    public static class RequestWrapper extends HttpServletRequestWrapper {
        Map<String, String> headers = new HashMap<String, String>();

        int contentLength;
        BufferedReader reader;

        public RequestWrapper(HttpServletRequest request) {
            super(request);
        }

        public void setHeader(String key, String value) {
            headers.put(key, value);
        }

        ByteArrayInputStream bais;
        public void setBody(String body) {
            bais = new ByteArrayInputStream(body.getBytes());
            contentLength = body.length();
            headers.put("content-length", Integer.toString(contentLength));
        }

        @Override
        public BufferedReader getReader() throws IOException {
            reader = new BufferedReader(new InputStreamReader(bais));
            return reader;
        }

        @Override
        public ServletInputStream getInputStream() throws IOException {
            return new ServletInputStream() {
                @Override
                public int read() throws IOException {
                    return bais.read();
                }
            };
        }

        @Override
        public String getMethod() {
            return "POST";
        }

        private String contentType;

        public void setContentType(String contentType) {
            this.contentType = contentType;
            headers.put("content-type", contentType);
        }

        @Override
        public String getContentType() {
            return contentType;
        }

        @Override
        public int getContentLength() {
            return contentLength;
        }

        @Override
        public String getHeader(String name) {
            String val = headers.get(name);
            if (val != null) {
                return val;
            }
            return super.getHeader(name);    //To change body of overridden methods use File | Settings | File Templates.
        }

        @Override
        public Enumeration getHeaders(final String name) {
            return super.getHeaders(name);
        }

        @Override
        public Enumeration getHeaderNames() {
            final Enumeration en1 = super.getHeaderNames();
            final Iterator it = headers.keySet().iterator();
            return new Enumeration() {
                public boolean hasMoreElements() {
                    return en1.hasMoreElements() || it.hasNext();
                }

                public Object nextElement() {
                    return en1.hasMoreElements() ? en1.nextElement() : (it.hasNext() ? it.next() : null);
                }
            };
        }

        @Override
        public int getIntHeader(String name) {
            String val = headers.get(name);
            if (val == null) {
                return super.getIntHeader(name);
            } else {
                return Integer.parseInt(val);
            }
        }
    }
}

<强>的web.xml

<filter>
    <filter-name>JSONPRequestFilter</filter-name>
    <filter-class>xxxxx.JSONPRequestFilter</filter-class>
    <init-param>
        <param-name>callbackParameter</param-name>
        <param-value>callback</param-value>
    </init-param>
</filter>

<filter-mapping>
  <filter-name>JSONPRequestFilter</filter-name>
  <url-pattern>/rest/*</url-pattern>
</filter-mapping>

答案 2 :(得分:2)

计划在RESTEasy 2.3.6 Final / 3.0-beta-4(https://issues.jboss.org/browse/RESTEASY-342)中发布支持JSONP的增强功能。通过简单地复制their code from GitHub,我能够“反向”使用RESTEasy 2.3.5的项目。

RESTEasy会根据注释自动获取新的提供者。一旦在url中看到名为“callback”的查询参数,它就会自动将结果包装在js回调中。这与JQuery发送给服务器的JSONP请求兼容。

答案 3 :(得分:1)

要从@talawahdotnet继续,我使用RestEasy 3.0.9.Final并支持JSONP,一旦启用,任何带有&#34;回调的请求&#34;查询参数将被包装为JSONP。我使用JBoss,因此其他容器的完整文档为here。这是我必须要做的步骤:

  1. 在您的web.xml中添加:

    <context-param>
        <param-name>resteasy.providers</param-name>
        <param-value>org.jboss.resteasy.plugins.providers.jackson.JacksonJsonpInterceptor</param-value>
    </context-param>
    
  2. 确保您的WEB-INF / jboss-deployment-structure.xml包含:

    <jboss-deployment-structure>
        <deployment>
            <dependencies>
                <module name="org.jboss.resteasy.resteasy-jackson-provider" services="import" annotations="true"/>
            </dependencies>
        </deployment>
    </jboss-deployment-structure>
    
  3. 确保你的pom.xml中有一个resteasy-jackson-provider依赖项,如:

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <scope>provided</scope>
    </dependency>
    

答案 4 :(得分:1)

在3.x版本中开箱即用claims to support JSONP

  

如果您正在使用杰克逊,Resteasy拥有您可以打开的JSONP   添加提供者   org.jboss.resteasy.plugins.providers.jackson.JacksonJsonpInterceptor   (Jackson2JsonpInterceptor,如果您使用的是Jackson2提供商)   你的部署。如果响应的媒体类型是json和a   给出回调查询参数,响应将是一个javascript   使用回调定义的方法的方法调用的代码段   参数。例如:

     

GET / resources / stuff?callback = processStuffResponse会产生这个   响应:

     

processStuffResponse()这支持默认值   jQuery的行为。

     

您可以通过设置更改回调参数的名称   callbackQueryParameter属性。

但是,由于RESTEASY-1168: Jackson2JsonpInterceptor does not render closing bracket

,它似乎已被破坏

所以 foo({"foo":"bar"} 被渲染而不是 foo({"foo":"bar"})

这会导致“Uncaught SyntaxError:Unexpected Identifier”错误

我已经提交了pull-request修复程序,希望它能够进入下一版本3.0.12

我知道这个问题已经过时了,但是当你搜索重新出现的jsonp问题时,它会显示在Google的第一页上,所以我决定更新它