"Java" Jackson parsing the pit of XML

Source: Internet
Author: User

To get the XML data, Jackson was configured for @ResponseBody in spring MVC.

Just used when the heart is ecstatic, and finally do not have to resolve their own ...

But ......... .............. There is still a hole ...

Pit One: Capitalization problems

@JacksonXmlProperty (localname = "Apid")

Private Integer Apid;

Private Integer Getapid ()

{

return this. Apid

}

Placing annotations on the member variables will parse out two Apid fields, one is <apid></apid>, and the other is <apid><apid>

This is because Jackson's processing mechanism automatically gets the member variable name from the property method, but in Java, either the hump is named, or the first two letters are capitalized in order to get the property correctly with the Get method, so the member name obtained with Getapid is Apid, Was parsed out by Jackson. And because the member variables are annotated, they are also parsed. This resulted in an XML file generating two apid tags. The correct approach is to write the annotations to the Get method

Correct wording:

Private Integer Apid;

@JacksonXmlProperty (localname = "Apid")

Private Integer Getapid ()

{

return this. Apid

}

Pit II: Jackson Package list problem

This problem is extremely difficult to troubleshoot ... began to think that the problem of encapsulation .... (Spit It out, this outsourced project uses a lot of XML interaction, but it doesn't use WebService to parse the encapsulated XML in a fixed format ..... Change also thief difficult ... )...... And finally, it was Jackson's problem.

Private list<integer> APID;

@JacksonXmlProperty (localname = "APID")
@JacksonXmlElementWrapper (usewrapping = False)
Public list<integer> Getapid () {
return APID;
}

Start Apid This list has been packaged on two levels! The correct result should be <APID>111</APID>

But what I get is <APID><APID>111</APID></APID>.

The problem is in jacksonxmlelementwrapper ............... This value defaults to true if you do not specify ... ..... ..... ..... ..... ..... ..... ................. ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ...... .....

True when Jackson encapsulates XML directly with Xmlproperty's localname and sealed a layer ...

It would be nice to change this to false.

"Java" Jackson parsing the pit of XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.