MOXY在字段名称

时间:2017-02-20 22:07:35

标签: java json moxy

我正在使用Moxy来序列化基于JSON的API。 除了一件小事之外,它就像一个魅力: 使用URL作为fieldName序列化字段不起作用。

这就是我想要实现的目标:

让Foo.class:

package test;

import javax.xml.bind.annotation.XmlElement;

public class Foo {

@XmlElement(name = "http://www.foo.bar")
private String bar;

private String foo;

@XmlElement(name = "_foo_bar")
private String fooBar;

public String getBar() {
    return bar;
}

public void setBar(String bar) {
    this.bar = bar;
}

public String getFoo() {
    return foo;
}

public void setFoo(String foo) {
    this.foo = foo;
}

public String getFooBar() {
    return fooBar;
}

public void setFooBar(String fooBar) {
    this.fooBar = fooBar;
}

}

测试类:

 package test;

 import java.io.StringReader;
 import java.util.HashMap;
 import java.util.Map;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.transform.stream.StreamSource;
 import org.eclipse.persistence.jaxb.JAXBContextProperties;

 public class TestMoxy {

public static void main(String[] args) {
    String json = "{\"http://www.foo.bar\":\"BAR\",\"foo\":\"FOO\",\"_foo_bar\":\"FOOBAR\"}";
    System.out.println(json);

    Foo f = unMarshal(json);
    System.out.println( f.getBar());
    System.out.println( f.getFoo());
    System.out.println( f.getFooBar());
}

public static Foo unMarshal(final String jsonString) {
    try {
        Map<String, Object> properties = new HashMap<String, Object>(2);
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);

        JAXBContext jc = JAXBContext.newInstance(new Class[] {Foo.class}, properties);      
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StringReader reader = new StringReader(jsonString);
        Foo f = unmarshaller.unmarshal(new StreamSource(reader),Foo.class).getValue();
        return f;
    } catch (final JAXBException e) {
        e.printStackTrace();
    }
    return null;
}
}

现在输出如下:

{"http://www.foo.bar":"BAR","foo":"FOO","_foo_bar":"FOOBAR"}
null
FOO
FOOBAR

所以,问题是:如何修复第一个属性的解组?

1 个答案:

答案 0 :(得分:0)

这看起来像MOXy中的一个错误,我测试了一些特殊字符的不同组合,MOXy似乎不喜欢:\\,这些抛出异常;其中http://https://甚至http//似乎被忽略(使用依赖项org.eclipse.persistence:org.eclipse.persistence.moxy:2.6.0。如果您确实需要能够处理形成为类似的属性名称URL,然后可能会看看其他库。我喜欢FasterXML Jackson

我能够让你的例子与杰克逊很快地合作。

Maven依赖:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.0</version>
</dependency>

让Foo.class:

package test;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Foo {

    @JsonProperty(value = "http://www.foo.bar")
    private String bar;

    private String foo;

    @JsonProperty(value = "_foo_bar")
    private String fooBar;

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }

    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public String getFooBar() {
        return fooBar;
    }

    public void setFooBar(String fooBar) {
        this.fooBar = fooBar;
    }
}

主要/测试类

package test;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;

public class TestMoxy {

    public static void main(String[] args) {
        String json = "{\"http://www.foo.bar\":\"BAR\",\"foo\":\"FOO\",\"_foo_bar\":\"FOOBAR\"}";
        System.out.println(json);

        Foo f = unMarshal(json);
        System.out.println(f.getBar());
        System.out.println(f.getFoo());
        System.out.println(f.getFooBar());
    }

    public static Foo unMarshal(String jsonString) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readValue(jsonString, Foo.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Foo();
    }
}