通过 Jettison/Resteasy 解组 JSON 数组

发布于 2024-10-10 01:05:18 字数 4635 浏览 0 评论 0原文

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

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

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

我要么需要隔离 Jettison bug,要么编写 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()));
    }
}

Ran into a similar problem like the following forum post:

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

Using Resteasy 2.0.1GA with Jettison 1.2 and getting a problem marshalling arrays when involving namespace mappings. See code below. Basically if the number of array entries are greater than one and namespace mappings are used. Anybody else run into this problem? The Nabble form poster got around it by writing a custom unmarshaller.

I either need to isolate the Jettison bug or write a Resteasy extension of the JettisonMappedUnmarshaller class (which hands over the namespace mappings and unmarshaller to the Jettison Configuration).

The following code doesn't unmarshall (post step) if the properties variables contains 2 or more entries.


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()));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半边脸i 2024-10-17 01:05:18

一定要使用Jettison吗?如果没有,我建议改用 Jackson 代替;这通常可以解决与数组/列表相关的问题(Jettison 的问题是它转换为 XML 模型,这使得很难区分数组和对象 —— 也存在错误,但从根本上来说很难正常工作)。

Do you have to use Jettison? If not I would recommend just switching to use Jackson instead; this typically solves array/list related problems (problem with Jettison is that it converts to XML model, which makes it very hard to tell arrays from objects -- there are bugs, too, but it is fundamentally hard thing to get working correctly).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文