Integrate JSON and XML view 2 in SpringMVC

Source: Internet
Author: User

The previous article introduced preparations for program integration, combined with the MarshallingView view to complete jaxb2 conversion XML and xStream conversion XML. This article will introduce the conversion of castor and jibx into XML.

In addition, MappingJacksonView converts JSON with Jackson and expands AbstractView to define the Jsonlib view to complete JSON-lib conversion.

4. Use Castor to convert XML

1. castor describes the Java object to be converted through a mapping. xml file, and then outputs the XML content according to the description of the Java object. To use castor to convert xml, add the following jar package:

 

If you are not clear about castor, read:

For csblogs: http://www.cnblogs.com/hoojo/archive/2011/04/25/2026819.html

For csdn: aspx "> http://blog.csdn.net/IBM_hoojo/archive/2011/04/25/6360916.aspx

2. You need to add the castor view to dispatcher. xml. The configuration is as follows:

<--
Inherit MarshallingView and override the locateToBeMarshalled method;
Fixed the bug where the converted xml is the BindingResult information when the object is added to ModelAndView.
-->
<Bean name = "castoremediallingview" class = "com. hoo. veiw. xml. overridemo-allingview">
<Property name = "marshaller">
<Bean class = "org. springframework. oxm. castor. castorgatealler">
<Property name = "mappingLocations">
<Array>
<Value> classpath: mapping. xml </value>
</Array>
</Property>
<Property name = "encoding" value = "UTF-8"/>
</Bean>
</Property>
</Bean>

Mapping. xml configuration

<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE mapping PUBLIC "-// EXOLAB/Castor Mapping DTD Version 1.0 //" http://castor.org/mapping.dtd ">
<Mapping>
<Class name = "com. hoo. entity. Account" auto-complete = "true">
<Map-to xml = "Account"/>
 
<Field name = "id" type = "integer">
<Bind-xml name = "id" node = "attribute"/>
</Field>
 
<Field name = "name" type = "string">
<Bind-xml name = "name" node = "element"/>
</Field>
 
<Field name = "email" type = "string">
<Bind-xml name = "email" node = "element"/>
</Field>
 
<Field name = "address" type = "string">
<Bind-xml name = "address" node = "element"/>
</Field>
 
<Field name = "brithday" type = "com. hoo. entity. Brithday">
<Bind-xml name = "Birthday" node = "element"/>
</Field>
</Class>
 
<Class name = "com. hoo. entity. Brithday" auto-complete = "true">
<Map-to xml = "brithday"/>
 
<Field name = "brithday" type = "string">
<Bind-xml name = "brithday" node = "attribute"/>
</Field>
</Class>

<Class name = "com. hoo. entity. MapBean" auto-complete = "true">
<Field name = "map" collection = "map">
<Bind-xml name = "map">
<Class name = "org. exolab. castor. mapping. MapItem">
<Field name = "key" type = "java. lang. String">
<Bind-xml name = "key" node = "attribute"/>
</Field>
<Field name = "value" type = "com. hoo. entity. Account">
<Bind-xml name = "value" auto-naming = "deriveByClass"/>
</Field>
</Class>
</Bind-xml>
</Field>
</Class>

<Class name = "com. hoo. entity. ListBean" auto-complete = "true">
<Map-to xml = "listBean"/>
<Field name = "list" collection = "arraylist" type = "com. hoo. entity. Account">
<Bind-xml name = "beans" auto-naming = "deriveByClass"/>
</Field>
<Field name = "name" type = "string"/>
</Class>

<Class name = "com. hoo. entity. AccountArray" auto-complete = "true">
<Map-to xml = "account-array"/>
<Field name = "size" type = "int"/>
<Field name = "accounts" collection = "array" type = "com. hoo. entity. Account">
<Bind-xml name = "accounts" auto-naming = "deriveByClass"/>
</Field>
</Class>
</Mapping>

For introduction to mapping. xml configuration, you can refer to html "> http://www.cnblogs.com/hoojo/archive/2011/04/25/2026819.html

The third topic of this article.

3. When using Spring MarshallingView, the converted xml results sometimes contain information about the BindingResult object. Therefore, the solution is to override the locateToBeMarshalled method in the MarshallingView to solve the problem. Here is the re-Marsh

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.