如何使用Gson解析来自InputStream的JSON对象数组

时间:2017-01-03 20:39:32

标签: java json filter gson

我有一个要求,我需要使用ClientResponseFilter拦截REST服务器的响应。服务器正在发送JSON对象数组,如下例所示。

[
    {
        "id":101,
        "fname":"Mike",
        "lname":"Smith",
        "age":25,
        "country":"France"
    },
    {
        "id":102,
        "fname":"Emily",
        "lname":"Clarke",
        "age":26,
        "country":"Canada"
    },
    {
        "id":103,
        "fname":"Jennifer",
        "lname":"Carsova",
        "age":27,
        "country":"Germany"
    }
]

我有一个要求,我需要在每个对象中添加一个名为URL的新属性。 URL字段的值类似于www.abc.com

我需要在我的过滤器中覆盖此方法 public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext)
ClientResponseContext有一个名为getEntityStream()的方法,它为响应内容提供输入流。一旦我能够将新属性添加到每个JSON对象,我将使用setEntityStream()方法传递新流(现在添加了属性)。
我正在使用Gson来实现这一点。下面是我尝试过的代码,但它不起作用。

public class SomeFilter implements ClientResponseFilter {

    @Override
    public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {

        JsonReader jsonReader = new JsonReader(new InputStreamReader(responseContext.getEntityStream(), "UTF-8"));
        JsonArray jsonArray = new JsonArray();
        JsonParser jsonParser = new JsonParser();
        JsonElement jsonElement;
        JsonObject jsonObject;

        while(jsonReader.hasNext()) {

            jsonReader.beginObject();        // I tried using jsonReader.beginArray() as well but this also does not work

            while(jsonReader.hasNext()) {
                jsonElement = jsonParser.parse(jsonReader);     // Throws an IllegalArgumentException here
                jsonObject = jsonElement.getAsJsonObject();
                jsonObject.addProperty("URL", "www.abc.com");
                jsonArray.add(jsonObject);
            }
        }

        InputStream inputStream = new ByteArrayInputStream(jsonArray.toString().getBytes());
        responseContext.setEntityStream(inputStream);

    }
}

以下是我得到的错误。

15:04:38,070 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/cq/release-management].[com.jackson.release.management.rest.application.ReleaseManagementApplication]](http-localhost/127.0.0.1:8080-1) JBWEB000236: Servlet.service() for servlet com.jackson.release.management.rest.application.ReleaseManagementApplication threw exception: org.jboss.resteasy.spi.UnhandledException: javax.ws.rs.ProcessingException
    at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:76) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:212) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:149) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:372) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) [resteasy-jaxrs-3.0.8.Final.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at com.jackson.sdlc.service.security.PrismSecurityContextFilter.doFilter(PrismSecurityContextFilter.java:57) [prism-util-1.1.0.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:231) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:512) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:926) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_77]
Caused by: javax.ws.rs.ProcessingException
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:264) [jersey-client-2.22.1.jar:]
    at org.glassfish.jersey.client.JerseyInvocation$3.call(JerseyInvocation.java:722) [jersey-client-2.22.1.jar:]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315) [jersey-common-2.22.1.jar:]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297) [jersey-common-2.22.1.jar:]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228) [jersey-common-2.22.1.jar:]
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444) [jersey-common-2.22.1.jar:]
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:718) [jersey-client-2.22.1.jar:]
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:430) [jersey-client-2.22.1.jar:]
    at org.glassfish.jersey.client.proxy.WebResourceFactory.invoke(WebResourceFactory.java:345) [jersey-proxy-client-2.22.1.jar:]
    at com.sun.proxy.$Proxy223.getProjectsByReleaseDate(Unknown Source)
    at com.jackson.workfront.service.WorkfrontProjectServiceImpl.getProjectsByReleaseDate(WorkfrontProjectServiceImpl.java:69) [workfront-rest-client-impl-1.1.1-SNAPSHOT.jar:]
    at com.jackson.release.management.impl.ProjectServiceRestImpl.getProjectsRelatedToRelease(ProjectServiceRestImpl.java:30) [classes:]
    at com.jackson.release.management.impl.ProjectServiceRestImpl$Proxy$_$$_WeldClientProxy.getProjectsRelatedToRelease(ProjectServiceRestImpl$Proxy$_$$_WeldClientProxy.java) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_77]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_77]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_77]
    at java.lang.reflect.Method.invoke(Method.java:498) [rt.jar:1.8.0_77]
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:296) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:250) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:237) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356) [resteasy-jaxrs-3.0.8.Final.jar:]
    ... 22 more
Caused by: java.lang.IllegalArgumentException
    at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:676) [gson-2.2.4.jar:]
    at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:642) [gson-2.2.4.jar:]
    at com.google.gson.internal.Streams.parse(Streams.java:44) [gson-2.2.4.jar:]
    at com.google.gson.JsonParser.parse(JsonParser.java:84) [gson-2.2.4.jar:]
    at com.jackson.workfront.client.filter.SomeFilter.filter(SomeFilter.java:58) [workfront-rest-client-impl-1.1.1-SNAPSHOT.jar:]
    at org.glassfish.jersey.client.ClientFilteringStages$ResponseFilterStage.apply(ClientFilteringStages.java:140) [jersey-client-2.22.1.jar:]
    at org.glassfish.jersey.client.ClientFilteringStages$ResponseFilterStage.apply(ClientFilteringStages.java:128) [jersey-client-2.22.1.jar:]
    at org.glassfish.jersey.process.internal.Stages.process(Stages.java:171) [jersey-common-2.22.1.jar:]
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:260) [jersey-client-2.22.1.jar:]
    ... 43 more

我已经尝试过在stackoverflow上找到的所有其他答案,但没有一个能够正常工作。

有人可以让我知道我的代码有什么问题以及如何解决它?

2 个答案:

答案 0 :(得分:0)

为什么不直接将流传递给解析器,只使用解析器?

答案 1 :(得分:0)

我不认为你有效地使用Gson。您可以使用的功能之一是它如何将JSON编组为Java对象。因此,对于您的示例JSON,我想像下面的类将代表一个人:

public class Person {
    int id;
    String fname;
    String lname;
    int age;
    String country;

    @SerializedName("URL")
    String url;
}

从这里,您可以告诉Gson从您的InputStream反序列化这些对象的列表:

InputStream stream = responseContext.getEntityStream();
Gson gson = new GsonBuilder().create();

Type type = new TypeToken<List<Person>>(){}.getType();
List<Person> people = gson.fromJson(new InputStreamReader(stream, StandardCharsets.UTF_8), type);

for(Person person : people) {
    person.url = "www.abc.com";
}

String json = gson.toJson(people);
InputStream inputStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
responseContext.setEntityStream(inputStream);