0

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

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

将 Resteasy 2.0.1GA 与 Jettison 1.2 一起使用,并在涉及命名空间映射时遇到编组数组的问题。请参阅下面的代码。基本上,如果数组条目的数量大于一并且使用命名空间映射。还有其他人遇到这个问题吗?Nabble 表单海报通过编写自定义解组器绕过它。

我要么需要隔离 Jettison 错误,要么编写 JettisonMappedUnmarshaller 类的 Resteasy 扩展(它将命名空间映射和解组器移交给 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()));
    }
}
4

1 回答 1

1

你必须使用 Jettison 吗?如果不是,我建议改用Jackson;这通常可以解决与数组/列表相关的问题(Jettison 的问题是它转换为 XML 模型,这使得从对象中区分数组非常困难——也有错误,但从根本上说很难正常工作)。

于 2011-01-06T18:26:50.103 回答