I have to take a java object and serialize it to a xml string. The expect xml must look like this:
<close_trailer_results>
<detl_code_load_cond_stat>CLSD</detl_code_load_cond_stat>
<detl_code_serv_type>GND</detl_code_serv_type>
<detl_code_due_sort>PRE</detl_code_due_sort>
<due_dt_gmt>2018-12-12 08:00:00</due_dt_gmt>
<org_id_next_sort>441</org_id_next_sort>
</close_trailer_results>
The actual xml looks like this:
> <close_trailer_results>
> <LoadCloseTrailerResponseXml>
> <detl_code_load_cond_stat>CLSD</detl_code_load_cond_stat>
> <detl_code_serv_type>OVN</detl_code_serv_type>
> <detl_code_due_sort>MID</detl_code_due_sort>
> <due_dt_gmt>2019-03-19 07:59:43</due_dt_gmt>
> <org_id_next_sort>61</org_id_next_sort>
> </LoadCloseTrailerResponseXml> </close_trailer_results
The java class maps the xml elements. But I want the class to be unwrapped. I don't want the class name in the xml document. This is the class
@JsonPropertyOrder({"detl_code_load_cond_stat", "detl_code_serv_type", "detl_code_due_sort", "due_dt_gmt", "org_id_next_sort"})
public class LoadCloseTrailerResponseXml {
private String detl_code_load_cond_stat;
private String detl_code_serv_type;
private String detl_code_due_sort;
private String due_dt_gmt;
private Integer org_id_next_sort;
...just getters and setters
}
When creating the xml this is the code:
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.setSerializationInclusion(Include.NON_NULL);
LoadCloseTrailerResponseXml responseTrailer = new LoadCloseTrailerResponseXml();
responseTrailer.setDetl_code_load_cond_stat(trailerResponse.getTrailer().getDetlCodeLoadCondStat());
responseTrailer.setDetl_code_serv_type(trailerResponse.getTrailer().getDetlCodeServType());
responseTrailer.setDetl_code_due_sort(trailerResponse.getTrailer().getDetlCodeDueSort());
responseTrailer.setDue_dt_gmt(trailerResponse.getTrailer().getDueDateGmt());
responseTrailer.setOrg_id_next_sort(trailerResponse.getTrailer().getOrgIdNextSort());
I was not able to add @JsonUnwrapped annotation at the class level. I tried adding this code before the xml is created:
JacksonXmlModule xmlModule = new JacksonXmlModule();
xmlModule.setDefaultUseWrapper(false);
ObjectMapper xmlMapper = new XmlMapper(xmlModule);
This had no affect. The class wrapping was still in the xml. How can I create the xml string without the wrapping of the class around the elements?
UPDATE I found a configuration setting for xmlMapper that should work but does not:
xmlMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
I read online that when creating an xml file from POJO, Jackson will put a root tag in the xml file whether you set one or not. If you don't set it, it will be the class name. My solution was to create the xml string that has these root tags and doing a string function to remove the root tags.