Spring3.0+jsonview/jackson

Source: Internet
Author: User


First, web.xml configuration:

<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_3.dt D "> <web-app> <display-name>archetype Created Web application</display-name> <context -param> <param-name>contextConfigLocation</param-name> <param-value> Classpath*:sprin G/*.xml </param-value> </context-param> <!--Filter encoding--> <filter> <fil Ter-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> <listener> <listeNer-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> & Lt;servlet> <servlet-name>spring</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name> contextconfiglocation</param-name> <param-value> Classpath*:spring/spring-p2p-console.xml </ param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <
	Servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.con</url-pattern>
 </servlet-mapping> </web-app>


Second, spring-servlet.xml configuration

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: 
	context= "Http://www.springframework.org/schema/context" xmlns:mvc= "Http://www.springframework.org/schema/mvc" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/be Ans/spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/sch Ema/context/spring-context-3.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.or G/schema/mvc/spring-mvc-3.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/ Tx/spring-tx-3.0.xsd "> <context:annotation-config/> <tx:annotation-driven transaction-manager=" tr Ansactionmanager "/> <context:component-scan BasE-package= "Com.icardpay.business.p2p.console.controller"/> <bean id= "DataSource" Org.apache.commons.dbcp.BasicDataSource "destroy-method=" Close "> <property name=" driverclassname "value=" Oracle.jdbc.driver.OracleDriver "/> <property name=" url "value=" JDBC:ORACLE:THIN:@192.168.1.8:1521:ORCL "/
	
	> <property name= "username" value= "root"/> <property name= "password" value= "root"/> </bean> <bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name= "Locations" > <list> <value>classpath:project.pro perties</value> </list> </property> </bean> <!--<bean CLA ss= "Org.springframework.web.servlet.view.BeanNameViewResolver" > <property name= "Order" value= "1"/> &L t;/bean> <bean name= "Jsonview" class= "Org.springframework". Web.servlet.view.json.JsonView "> <property name=" jsonwriter "> <ref bean=" jsonwriter "/> </proper ty> </bean> <bean name= "Jsonwriter" class= " Org.springframework.web.servlet.view.json.writer.sojo.SojoJsonStringWriter "> <property name=" Convertallmapvalues "> <value>true</value> </property> </bean>-->//comments out for Jsonview &L T;bean class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <propert Y name= "messageconverters" > <list > <ref bean= "Mappingjacksonhttpmessa" Geconverter "/> </list> </property> </bean> <bean I D= "Mappingjacksonhttpmessageconverter" class= " Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter "> <property name=" Supportedmedi Atypes "> <list> <value>application/json;charset=utf-8</value> </list> </property> </bean> </beans>

Third, Pom.xml


<!--json-->
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			< artifactid>jackson-core-lgpl</artifactid>
		</dependency>

		<dependency>
			< Groupid>org.codehaus.jackson</groupid>
			<artifactId>jackson-mapper-lgpl</artifactId>
		</dependency>



Four, controller code


@Controller
@RequestMapping ("/xxx") public
class Aaaaacontroller {
	
	private static Logger Logger = Loggerfactory.getlogger (aaaaacontroller.class);

	@Resource
	private ffffbiz xxxxeeee;
	
    @RequestMapping ("/test_json")
    @ResponseBody public  
    map<string,object>  testJson3 (Param p) {
    	System.out.println ("Enter ...");
    	System.out.println ("value....==========" +p.getid ());
    	 map<string,object> map = new hashmap<string,object> ();  
         Map.put ("code", true);  
    	return map;
    }
	







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.