我有一个以JSON和XML格式返回对象的服务。
http://localhost:8091/apiN/xml/2
XML结果
<restObjectList>
<restObjectList>
<restObjectList>
<timestamp>2017-06-19 17:01:01</timestamp>
<title>Rest object</title>
<fullText>This is the full text. ID: 1</fullText>
<id>1</id>
<value>0.1412789210135622</value>
</restObjectList>
<restObjectList>
<timestamp>2017-06-19 17:01:01</timestamp>
<title>Rest object</title>
<fullText>This is the full text. ID: 2</fullText>
<id>2</id>
<value>0.9886539664938628</value>
</restObjectList>
</restObjectList>
</restObjectList>
http://localhost:8091/apiN/2
JSON结果
{
"restObjectList": [
{
"timestamp": "2017-06-19 17:01:01",
"title": "Rest object",
"fullText": "This is the full text. ID: 1",
"id": 1,
"value": 0.1412789210135622
},
{
"timestamp": "2017-06-19 17:01:01",
"title": "Rest object",
"fullText": "This is the full text. ID: 2",
"id": 2,
"value": 0.9886539664938628
}
]
}
结果我想收到
XML
<restObjectList>
<restObject>
<timestamp>2017-06-19 17:01:01</timestamp>
<title>Rest object</title>
<fullText>This is the full text. ID: 1</fullText>
<id>1</id>
<value>0.1412789210135622</value>
</restObject>
<restObject>
<timestamp>2017-06-19 17:01:01</timestamp>
<title>Rest object</title>
<fullText>This is the full text. ID: 2</fullText>
<id>2</id>
<value>0.9886539664938628</value>
</restObject>
</restObjectList>
JSON
{
"restObjectList": [{
"restObject": {
"timestamp": "2017-06-19 17:01:01",
"title": "Rest object",
"fullText": "This is the full text. ID: 1",
"id": 1,
"value": 0.1412789210135622
}
}, {
"restObject": {
"timestamp": "2017-06-19 17:01:01",
"title": "Rest object",
"fullText": "This is the full text. ID: 2",
"id": 2,
"value": 0.9886539664938628
}
}]
}
如何为JSON和XML包装restObject
并修复restObjectList
的XML数据,因为此标记会在不同级别重复。
我的代码
RestObject
@JsonRootName(value = "restObject")
@XmlRootElement(name = "restObject")
public class RestObject implements Serializable {
private LocalDateTime timestamp;
private String title;
private String fullText;
private Long id;
private Double value;
//Getters, setters
}
RestObjectList
@JsonRootName(value = "restObjectList")
@XmlSeeAlso({RestObject.class})
public class RestObjectList {
private List<RestObject> restObjectList;
//Getter and setter
}
JacksonConfig
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(true).build();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);//Use custom date-time format.
objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
return objectMapper;
}
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
objectMapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
return objectMapper;
}
}
答案 0 :(得分:2)
JSON和XML结构不相同。在XML中,您需要一个RestObject
列表,在JSON中,您需要一个列表,其元素将RestObject
的实例包装在另一个对象中。这不是一个简单的Jackson注释可以得到的东西,你需要一个仅用于JSON序列化的自定义序列化器。首先,获得所需的XML格式非常简单:
class RestObject implements Serializable {
private LocalDateTime timestamp;
private String title;
private String fullText;
private Long id;
private Double value;
}
@JsonRootName("restObjectList")
class RestObjectList {
@JacksonXmlProperty(localName = "restObject")
@JacksonXmlElementWrapper(useWrapping = false)
private List<RestObject> restObjectList;
}
如果您绝对想要在json中包装数组中的每个元素,那么您将需要一个自定义序列化程序,例如
class RestObjectSerializer extends JsonSerializer<RestObject> {
@Override
public void serialize(RestObject value, JsonGenerator gen, SerializerProvider serializers) throws
IOException {
gen.writeStartObject();
gen.writeObjectFieldStart("restObject");
gen.writeObjectField("timeStamp", value.getTimestamp());
gen.writeStringField("title", value.getTitle());
// other fields
gen.writeEndObject();
gen.writeEndObject();
}
}
仅注册序列化JSON的ObjectMapper
,因此它不会干扰序列化为XML:
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("MyModule");
module.addSerializer(RestObject.class, new RestObjectSerializer());
mapper.registerModule(module);