Integrate JSON and XML view 1 in SpringMVC

Source: Internet
Author: User

SpringMVC integrates JSON and XML views to convert Java objects to XML and JSON. XML Conversion provides MarshallingView. Developers only need to inject corresponding marshaller and attribute configurations to automatically group the data in the Java Model object to XML.

Email: hoojo_@126.com

Blog: http://blog.csdn.net/IBM_hoojo

Http://hoojo.cnblogs.com/

I. Preparations

1. This program involves Jackson, xStream, Jibx, Jaxb2, castor, and other technologies.

The content involved in this article should be of great help to you.

2. Download the jar package

Spring versions jar: http://ebr.springsource.com/repository/app/library/detail? Name = org. springframework. spring

The related dependency packages can also be found here: http://ebr.springsource.com/repository/app/library

3. At least the following jar packages are required

 

4. web. xml configuration of the current project

<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>

<-- Configure the Spring core controller -->
<Servlet>
<Servlet-name> dispatcher </servlet-name>
<Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>
<Init-param>
<Param-name> contextConfigLocation </param-name>
<Param-value>/WEB-INF/dispatcher. xml </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>

<Servlet-mapping>
<Servlet-name> dispatcher </servlet-name>
<Url-pattern> *. do </url-pattern>
</Servlet-mapping>

<-- Solve the Engineering Code filter -->
<Filter>
<Filter-name> characterEncodingFilter </filter-name>
<Filter-class> org. springframework. web. filter. CharacterEncodingFilter </filter-class>
<Init-param>
<Param-name> encoding </param-name>
<Param-value> UTF-8 </param-value>
</Init-param>
</Filter>

<Filter-mapping>
<Filter-name> characterEncodingFilter </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>

<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
</Web-app>

5. dispatcher. xml configuration in WEB-INF

<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: mvc = "http://www.springframework.org/schema/mvc"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: util = "http://www.springframework.org/schema/util"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://www.springframework.org/schema/util
Http://www.springframework.org/schema/util/spring-util-3.0.xsd>

<-- Annotation detector -->
<Context: component-scan base-package = "com. hoo. controller"/>

<-- View Parser: searches for the corresponding bean configuration in the configuration file based on The View name new ModelAndView (name) -->
<Bean class = "org. springframework. web. servlet. view. BeanNameViewResolver">
<Property name = "order" value = "1"/>
</Bean>

<Bean class = "org. springframework. web. servlet. view. InternalResourceViewResolver">
<Property name = "viewClass" value = "org. springframework. web. servlet. view. JstlView"/>
</Bean>
</Beans>

After startup, you can see that index. jsp has no exception or error. The current SpringMVC configuration is successful.


Ii. Use Jaxb2 to group XML

1. Jaxb2 can complete the conversion between XML and Java, which is used in WebService. We have also introduced the usage of Jaxb2.

Online blog:

For cnblogs: http://www.cnblogs.com/hoojo/archive/2011/04/26/2029011.html

For csdn: aspx "> http://blog.csdn.net/IBM_hoojo/archive/2011/04/26/6363491.aspx

2. Configure the marshaller view of Jaxb2 in dispatcher. xml as follows:

<-- Xml view, Jaxb2Marshaller. You need to configure the object and object to add the Annotation xml Annotation without adding additional jar packages -->

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.