首页 文章

通过Jettison / Resteasy解组JSON数组

提问于
浏览
0

遇到类似以下论坛帖子的类似问题:

http://jersey.576304.n2.nabble.com/parsing-JSON-with-Arrays-using-Jettison-td5732207.html

使用Resteasy 2.0.1GA和Jettison 1.2并在涉及命名空间映射时遇到问题编组数组 . 见下面的代码 . 基本上,如果数组条目的数量大于1,则使用名称空间映射 . 还有其他人遇到这个问题吗? Nabble形式的海报通过编写一个定制的unmarshaller绕过它 .

我要么需要隔离Jettison错误,要么编写JettisonMappedUnmarshaller类的Resteasy扩展(它将命名空间映射和unmarshaller移交给Jettison配置) .

如果属性变量包含2个或更多条目,则以下代码不会解组(后步骤) .

public class Experimenting {

    @Path("test")
    public static class MyResource {
        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "Property", propOrder = { "name", "value" })
        public static class MyProperty {
            @XmlElement(name = "Name", required = true)
            protected String name;
            @XmlElement(name = "Value", required = true)
            protected String value;

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getValue() {
                return value;
            }

            public void setValue(String value) {
                this.value = value;
            }
        }

        @XmlType(name = "MyElement", propOrder = { "myProperty" })
        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlRootElement(name = "MyElement", namespace = "http://www.klistret.com/cmdb/ci/commons")
        @Mapped(namespaceMap = { @XmlNsMap(namespace = "http://www.klistret.com/cmdb/ci/commons", jsonName = "com.klistret.cmdb.ci.commons") })
        public static class MyElement {
            @XmlElement(name = "MyProperty", namespace = "http://www.klistret.com/cmdb/ci/commons")
            protected List myProperty;

            public List getMyProperty() {
                if (myProperty == null) {
                    myProperty = new ArrayList();
                }
                return this.myProperty;
            }

            public void setMyProperty(List myProperty) {
                this.myProperty = myProperty;
            }
        }

        @GET
        @Path("myElement/{id}")
        @Produces(MediaType.APPLICATION_JSON)
        public MyElement getMy(@PathParam("id")
        Long id) {
            MyElement myElement = new MyElement();

            MyProperty example = new MyProperty();
            example.setName("example");
            example.setValue("of a property");

            MyProperty another = new MyProperty();
            another.setName("another");
            another.setValue("just a test");

            MyProperty[] properties = new MyProperty[] { example, another };
            myElement.setMyProperty(Arrays.asList(properties));

            return myElement;
        }

        @POST
        @Path("/myElement")
        @Consumes(MediaType.APPLICATION_JSON)
        @Produces(MediaType.APPLICATION_JSON)
        public MyElement createMy(MyElement myElement) {
            List properties = myElement.getMyProperty();
            System.out.println("Properties size: " + properties.size());

            return myElement;
        }
    }

    private Dispatcher dispatcher;

    @Before
    public void setUp() throws Exception {
        // embedded server
        dispatcher = MockDispatcherFactory.createDispatcher();
        dispatcher.getRegistry().addPerRequestResource(MyResource.class);

    }

    @Test
    public void getAndCreate() throws URISyntaxException,
            UnsupportedEncodingException {
        MockHttpRequest getRequest = MockHttpRequest.get("/test/element/44");
        MockHttpResponse getResponse = new MockHttpResponse();

        dispatcher.invoke(getRequest, getResponse);
        String getResponseBodyAsString = getResponse.getContentAsString();

        System.out.println(String.format(
                "Get Response code [%s] with payload [%s]", getResponse
                        .getStatus(), getResponse.getContentAsString()));

        MockHttpRequest postRequest = MockHttpRequest.post("/test/element");
        MockHttpResponse postResponse = new MockHttpResponse();

        postRequest.contentType(MediaType.APPLICATION_JSON);
        postRequest.content(getResponseBodyAsString.getBytes("UTF-8"));

        dispatcher.invoke(postRequest, postResponse);
        System.out.println(String.format(
                "Post Response code [%s] with payload [%s]", postResponse
                        .getStatus(), postResponse.getContentAsString()));
    }
}

1 回答

  • 1

    你必须使用Jettison吗?如果不是,我建议只改用Jackson;这通常解决了与数组/列表相关的问题(Jettison的问题在于它转换为XML模型,这使得很难从对象中分辨数组 - 也存在错误,但从根本上讲,正常工作很难) .

相关问题