Spring 配置Response自动转换为JSON

简述:

有时候希望Spring在controller中自动将组装好的消息对象转换为JSON格式输出


步骤:

1. 采用全注解配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

	<!-- Package Scan -->
	<context:component-scan base-package="com.anialy.webproj.*"></context:component-scan>

	<!-- Home Page Redirection -->
	<mvc:view-controller path="/" view-name="redirect:/home" />
	<mvc:view-controller path="/home" view-name="home" />

	<!-- Spring Service Invoke -->
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="/service/httpService">httpService</prop>
			</props>
		</property>
	</bean>


	<!-- Spring MVC -->
	<mvc:annotation-driven />

	<!-- 定义JSP -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>


</beans>


2. ResponseEntity

package com.anialy.webproj.common;

public class ResponseEntity<T> {
	private int state;
	private T payload;
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	public T getPayload() {
		return payload;
	}
	public void setPayload(T payload) {
		this.payload = payload;
	}
}


3. 

TestController.java

package com.anialy.webproj.controller;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.anialy.webproj.common.ResponseEntity;
import com.anialy.webproj.controller.base.BaseController;
import com.anialy.webproj.httpservice.entity.Person;

@Controller
@RequestMapping("/Test")
public class TestController extends BaseController{
	private static Logger logger 
         = LoggerFactory.getLogger(TestController.class);

	@SuppressWarnings("unchecked")
	@RequestMapping(value="/test/{name}", method=RequestMethod.GET
			, produces="application/json")
	public @ResponseBody ResponseEntity<Person> test(@PathVariable String name) {
		Person person = new Person(name, 23);
		logger.info("param is: {} !", name);
		responseEntity.setPayload(person);
		return responseEntity;
	}
}


3. web.xml

<?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">
	<display-name>WebProj</display-name>
	<servlet>
		<servlet-name>springServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:/applicationContext-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
            classpath:applicationContext-constants.xml
			classpath:applicationContext-dao.xml
            classpath:applicationContext-service.xml
            classpath:applicationContext-server-http-service.xml
		</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<session-config>
		<session-timeout>20</session-timeout>
	</session-config>
</web-app>


页面输出如下,

















你可能感兴趣的:(Spring 配置Response自动转换为JSON)