通过Jettison / Resteasy解组JSON数组

时间:2011-01-04 19:43:35

标签: json resteasy jettison

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

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

在Jettison 1.2中使用Resteasy 2.0.1GA并在涉及命名空间映射时遇到问题编组数组。见下面的代码。基本上,如果数组条目的数量大于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 个答案:

答案 0 :(得分:1)