如何在没有JAXBElement包装的情况下JSON-marshal JAXBElement包装的响应? [英] How to JSON-marshal JAXBElement-wrapped responses without the JAXBElement wrapper?
问题描述
我有一个使用Spring(v4.0.5)的http服务。它的http端点是使用Spring Web MVC配置的。响应是由模式生成的JAXB2-anotated类。响应包含在 JAXBElement
中,因为生成的JAXB类没有运行 @XmlRootElement
注释(并且架构无法修改去医生)。我不得不通过XML编组来进行一些战斗;在任何情况下,它都在工作。
I have an http service that is using Spring (v4.0.5). Its http endpoints are configured using Spring Web MVC. The responses are JAXB2-anotated classes that are generated off of a schema. The responses are wrapped in JAXBElement
as the generated JAXB classes do not sport @XmlRootElement
annotations (and the schema cannot be modified to doctor this). I had to fight a bit with getting XML marshalling ti work; in any case, it is working.
现在我正在设置JSON编组。我遇到的是获取具有 JAXBElement
信封的JSON文档。
Now I am setting up JSON marshalling. What I am running into is getting JSON-documents that feature the JAXBElement
"envelope".
{
"declaredType": "io.github.gv0tch0.sotaro.SayWhat",
"globalScope": true,
"name": "{urn:io:github:gv0tch0:sotaro}say",
"nil": false,
"scope": "javax.xml.bind.JAXBElement$GlobalScope",
"typeSubstituted": false,
"value": {
"what": "what",
"when": "2014-06-09T15:56:46Z"
}
}
我想要编组的只是值
-object:
What I would like to get marshalled instead is just the value
-object:
{
"what": "what",
"when": "2014-06-09T15:56:46Z"
}
这是我的JSON编组配置(弹簧上下文配置的一部分):
Here is my JSON marshalling config (part of the spring context configuration):
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jacksonMapper" />
<property name="supportedMediaTypes" value="application/json" />
</bean>
<bean id="jacksonMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd'T'HH:mm:ss'Z'" />
<property name="timeZone">
<bean class="java.util.TimeZone" factory-method="getTimeZone">
<constructor-arg type="java.lang.String" value="UTC" />
</bean>
</property>
</bean>
</property>
</bean>
我希望这可以通过配置 ObjectMapper $来实现C $ C>。我想也可以推出我自己的序列化器。思考?建议?
I am hoping that this can be accomplished by configuring the ObjectMapper
. I guess alternatively rolling out my own serializer may work. Thoughts? Suggestions?
推荐答案
您可以注册 mixin JAXBElement类的注释,它将放置注释,使其返回值为JSON表示。下面是一个示例:
You can register a mixin annotation for the JAXBElement class which would put the @JsonValue annotation on the JAXBElement.getValue() method making its return value to be the JSON representation. Here is an example:
一个示例.xsd chema文件,提供给 xjc
。
An example .xsd chema file that are given to xjc
.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="item" type="Thing"/>
<xs:complexType name="Thing">
<xs:sequence>
<xs:element name="number" type="xs:long"/>
<xs:element name="string" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Java主类:
public class JacksonJAXBElement {
// a mixin annotation that overrides the handling for the JAXBElement
public static interface JAXBElementMixin {
@JsonValue
Object getValue();
}
public static void main(String[] args) throws JAXBException, JsonProcessingException {
ObjectFactory factory = new ObjectFactory();
Thing thing = factory.createThing();
thing.setString("value");
thing.setNumber(123);
JAXBElement<Thing> orderJAXBElement = factory.createItem(thing);
System.out.println("XML:");
JAXBContext jc = JAXBContext.newInstance(Thing.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(orderJAXBElement, System.out);
System.out.println("JSON:");
ObjectMapper mapper = new ObjectMapper();
mapper.addMixInAnnotations(JAXBElement.class, JAXBElementMixin.class);
System.out.println(mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(orderJAXBElement));
}
}
输出:
XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<item>
<number>123</number>
<string>value</string>
</item>
JSON:
{
"number" : 123,
"string" : "value"
}
这篇关于如何在没有JAXBElement包装的情况下JSON-marshal JAXBElement包装的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!