2

使用 MOXy 我正在尝试将这样的 java 类编组为 JSON:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Request {
   String method;
   @XmlAnyElement(lax=true)
   Object[] arguments;
}

我希望是这样的:

{
    "method": "test",
    "arguments": ["a", "b"]
}

但 JSON 输出结果为:

{
    "method": "test",
    "value": ["a", "b"]
}

从哪里来value

如果我@XmlElementWrapper在参数字段上加上一个,它会变得更糟:

{
    "method":"test",
    "arguments":"a""value":["b"]
}

我的 JUnitTestCase看起来像这样:

import static org.junit.Assert.assertEquals;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.*;
import javax.xml.bind.annotation.*;

import org.junit.Test;

public class JsonRequestTest {

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public static class Request {
    String method;

    @XmlAnyElement(lax=true)
    Object[] arguments;
} // InvocationRequest

@Test
public void testObjectArray() throws JAXBException {
    System.setProperty(JAXBContext.class.getName(), "org.eclipse.persistence.jaxb.JAXBContextFactory");
    Map<String, Object> props= new HashMap<String, Object>();
    props.put("eclipselink.media-type", "application/json");
    props.put("eclipselink.json.include-root", false);

    JAXBContext ctx = JAXBContext.newInstance(new Class<?>[]{Request.class},props);
    Marshaller m = ctx.createMarshaller();
    StringWriter writer = new StringWriter();
    Request req = new Request(); 
    req.method="test";
    req.arguments = new Object[]{"a","b"};
    m.marshal(req, writer);
    assertEquals("{\"method\":\"test\", \"arguments\":[\"a\",\"b\"]}", writer.toString());
}
} // class JsonRequestTest
4

1 回答 1

1

注意: 我是EclipseLink MOXy的负责人,也是JAXB (JSR-222)专家组的成员。

TL:博士

您可以设置以下属性来覆盖value密钥。

    props.put(MarshallerProperties.JSON_VALUE_WRAPPER, "arguments");

完整的测试用例

下面是完整的工作测试用例。除了设置属性之外,我还删除了控制文档中的一个额外空间,以使测试通过。

import static org.junit.Assert.assertEquals;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.*;
import javax.xml.bind.annotation.*;

import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.junit.Test;

public class JsonRequestTest {

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Request {
        String method;

        @XmlAnyElement(lax = true)
        Object[] arguments;
    } // InvocationRequest

    @Test
    public void testObjectArray() throws JAXBException {
        System.setProperty(JAXBContext.class.getName(),
                "org.eclipse.persistence.jaxb.JAXBContextFactory");
        Map<String, Object> props = new HashMap<String, Object>();
        props.put("eclipselink.media-type", "application/json");
        props.put("eclipselink.json.include-root", false);
        props.put(MarshallerProperties.JSON_VALUE_WRAPPER, "arguments");

        JAXBContext ctx = JAXBContext.newInstance(
                new Class<?>[] { Request.class }, props);
        Marshaller m = ctx.createMarshaller();
        StringWriter writer = new StringWriter();
        Request req = new Request();
        req.method = "test";
        req.arguments = new Object[] { "a", "b" };
        m.marshal(req, writer);
        assertEquals("{\"method\":\"test\",\"arguments\":[\"a\",\"b\"]}",
                writer.toString());
    }

} // class JsonRequestTest

@XmlElementWrapper问题

我已经针对以下问题打开了以下错误@XmlElementWrapper

于 2013-11-18T15:14:39.223 回答