用Jackson解串JSONP

问题描述 投票:2回答:2

由于某些原因,Jackson 2.3.0无法解析JSONP响应。

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'my_json_callback':

我有反序列化过程可以在没有回调的情况下工作。

我尝试了包含@JSONP批注的Jackson JAX-RS程序包,但这似乎仅在序列化时使用。

java jsonp jackson
2个回答
3
投票

这是我使用ReaderInterceptor提出的解决方案的缩小版。我正在将Jersey 2.x与Jackson结合使用,以与仅输出JSONP的Web服务进行交互。

public class CallbackStripInterceptor implements ReaderInterceptor {

    private final static byte[] callbackBytes = "callback(".getBytes();

    @Override
    public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {

    int howMany = callbackBytes.length;

    InputStream x = context.getInputStream();

    if( !(x.available() >= howMany) ) {
        return context.proceed();
    }

    x.mark( howMany );
    byte[] preamble = new byte[ howMany ];
    x.read( preamble );

    // In case the first part of our entity doesn't have the callback String, reset the stream so downstream exploiters get the full entity.
    if( !Arrays.equals( preamble, callbackBytes ) ) {
        x.reset();
    } 

    return context.proceed();
}

像这样使用:

Client c = ClientBuilder.newBuilder()
    .register( new CallbackStripInterceptor() )
    .build();

使用此客户端,所有具有实体的响应都将通过此拦截器(Jersey不会在没有实体主体的响应上运行拦截器)。


2
投票

最后,我已经能够删除JSONP响应的回调部分。

[首先,Jackson能够解析JSON,即使它以括号结尾。因此,只需从响应中删除my_json_callback(就足够了。

由于我使用的是Apache的HTTP客户端,因此可以解决此问题:

String callback = "my_json_callback(";
InputStreamReader r = new InputStreamReader(response.getEntity().getContent());
r.skip(callback.length());
return mapper.readValue(r, MyObject.class);

这个想法是不必将Reader转换为String,然后在删除回调部分之后解析该String。

对于给定的JSONP字符串,我还可以使用JSONTokener库中的json.org实现相同的结果:

JSONTokener t = new JSONTokener(json);
t.nextValue(); // skip the callback
return mapper.readValue(t.nextValue().toString(), MyObject.class);
© www.soinside.com 2019 - 2024. All rights reserved.