我有一个带有 JAXB 注释的 POJO,它可以很好地编码到 xml 和 json,但是,xml 和 json 不兼容,因为根标记在 json 中丢失。我知道它在 json 中更有意义,但我仍然需要根元素。有办法获取吗?
@XmlRootElement(name = XmlConsts.ELEMENT)
@XmlAccessorType(XmlAccessType.FIELD)
public class Element {
private static final String MULTIPLE_VALUE_DELIMITER = ",";
@XmlElement(name = XmlConsts.PROPERTIES)
@XmlJavaTypeAdapter(JaxbMapAdapter.class)
private final Map<String, String> entries;
@XmlAttribute(name = XmlConsts.ELEMENT_TYPE, required = false)
public String getType() {
return entries.get(XmlConsts.ELEMENT_TYPE);
}
...
结果 xml:
<element type="rule.record">
<properties>
<property name="min" value="0"/>
<property name="id" value="1773"/>
...
结果 json:
{"type":"rule.record","properties":{
"property":[
{"name":"min","value":"0"},
{"name":"id","value":"1773"}
如此处所示,“element”标签在 json 中丢失。有没有办法获取根元素?
最佳答案
以下是根据您运行的环境可以完成此操作的几种方法:
独立示例
使用标准 JAXB API 时,MOXy 将在编码为 JSON 时包含根元素。可以使用 JAXBContextProperties.JSON_INCLUDE_ROOT
属性更改此行为。如果在创建 JAXBContext
时已将其设置为 false
,您可以在 Marshaller
上将其设置回 true
.
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.jaxb.MarshallerProperties;
public class Demo {
public static void main(String[] args) throws Exception {
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);
Foo foo = new Foo();
foo.setBar("Hello World");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
marshaller.marshal(foo, System.out);
}
}
JAX-RS 和 MOXyJsonProvider
MOXy 包含 MessageBodyReader
/MessageBodyWriter
的实现,可以轻松将 MOXy 配置为 JSON 绑定(bind)提供程序。默认情况下,MOXyJsonProvider
配置为不包含根元素。您可以通过在 JAX-RS Application
类上设置已配置的实例来更改此设置,如下所示。
import java.util.*;
import javax.ws.rs.core.Application;
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;
public class FooApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> set = new HashSet<Class<?>>(1);
set.add(ExampleService.class);
return set;
}
@Override
public Set<Object> getSingletons() {
MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();
moxyJsonProvider.setIncludeRoot(true);
HashSet<Object> set = new HashSet<Object>(1);
set.add(moxyJsonProvider);
return set;
}
}
了解更多信息
MOXy 作为 Jersey/GlassFish 中的默认 JSON 绑定(bind)提供程序
如果您使用 MOXy 作为 Jersey/GlassFish 的默认 JSON 绑定(bind)提供程序,您可以利用 MoxyJsonConfig
类,如下所示:
import javax.ws.rs.ext.*;
import org.glassfish.jersey.moxy.json.MoxyJsonConfig;
@Provider
public class MOXyJsonContextResolver implements ContextResolver<MoxyJsonConfig> {
private final MoxyJsonConfig config;
public MOXyJsonContextResolver() {
config = new MoxyJsonConfig()
.setIncludeRoot(true);
}
@Override
public MoxyJsonConfig getContext(Class<?> objectType) {
return config;
}
}
了解更多信息
关于java - MOXy 兼容 XML 和 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19103774/