JAXB 객체가 있습니다. 그것을 serialize 할 때 결과는 재미 있습니다! 이와 같이 = 소스 XML 그냥 가지고, 반면>Jackson이 JAXB 객체를 직렬화하면 이상한 결과가 발생합니다.
{"formData":{
"preConditions":{
"acceptTermsAndConditions":"<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<acceptTermsAndConditions>true</acceptTermsAndConditions>",
"receivePromoEmail":"<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<receivePromoEmail>false</receivePromoEmail>"
}, etc...
사실과 값으로 거짓 : -
<formData>
<preConditions>
<acceptTermsAndConditions>true</acceptTermsAndConditions>
<receivePromoEmail>false</receivePromoEmail>
</preConditions> etc...
다음과 같이 JSON을 생성하는 내 코드는 다음과 같습니다 -
Application application = (Application) JAXBUtil.getXMLAsApplication();
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
// make deserializer use JAXB annotations (only)
mapper.getDeserializationConfig().with(introspector);
// make serializer use JAXB annotations (only)
mapper.getSerializationConfig().with(introspector);
try {
mapper.writeValue(new File("application.json"), application);
} catch (IOException e) {
e.printStackTrace();
}
여기서, 위의 PreConditions 클래스는 J AXB2 (XJC). 다음은 스 니펫입니다. -
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"acceptTermsAndConditions",
"receivePromoEmail"
})
public static class PreConditions {
@XmlElement(required = true)
protected Object acceptTermsAndConditions;
@XmlElement(required = true)
protected Object receivePromoEmail;
/**
* Gets the value of the acceptTermsAndConditions property.
*
* @return
* possible object is
* {@link Object }
*
*/
public Object getAcceptTermsAndConditions() {
return acceptTermsAndConditions;
}
/**
* Sets the value of the acceptTermsAndConditions property.
*
* @param value
* allowed object is
* {@link Object }
*
*/
public void setAcceptTermsAndConditions(Object value) {
this.acceptTermsAndConditions = value;
}
/**
* Gets the value of the receivePromoEmail property.
*
* @return
* possible object is
* {@link Object }
*
*/
public Object getReceivePromoEmail() {
return receivePromoEmail;
}
/**
* Sets the value of the receivePromoEmail property.
*
* @param value
* allowed object is
* {@link Object }
*
*/
public void setReceivePromoEmail(Object value) {
this.receivePromoEmail = value;
}
}
JSON이 너무 미친 이유에 대한 단서가 있습니까?
이 정보는 충분하지 않습니다. 'Application'유형의 정의를 추가 할 수 있습니까? 내 생각 엔 "acceptTermsAndConditions"와 "receivePromoEmail"은 DOM 문서 또는 그와 같은 POJO 유형이 아니며 원시 문자열로만 직렬화 될 수 있습니다. – StaxMan
요청에 따라 응용 프로그램 유형에 대한 정보로 업데이트되었습니다. – Nebu
아. 자, 문제는'Application.FormData' ("이상한"JSON이있는 곳)에 있습니다. 그것도 추가 할 수 있습니까? 나는 여전히 유형이 DOM'Element' 또는 XML을 덤핑하는 것 이외의 다른 방법으로는 처리 할 수없는 다른 XML 특정 유형으로 선언된다고 생각합니다. – StaxMan