Spring对json的处理方式一

闲话不多说,最近在看返回json的这种方式 发现网上的 做法 比较多 ,也比较杂,个人试试。如下配置即可实现
spring-servlet.xml

 
<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
	<mvc:annotation-driven />
	<context:annotation-config></context:annotation-config>
	<context:component-scan base-package="com.julongtech"></context:component-scan>

	<!-- 视图模式 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="order" value="100"></property>
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/" />
		<property name="suffix" value=".jsp" />
		<property name="contentType" value="text/html; charset=utf-8" />
	</bean>
	<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
		<property name="order" value="10"></property>
	</bean>



</beans>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  
      <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.action</url-pattern>
        <url-pattern>*.json</url-pattern>
    </servlet-mapping>
  
</web-app>

 

HelloAction

package com.julongtech.action;

import java.util.Hashtable;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
public class HelloAction {
	
	/**
	 * 此种方式可以不用做任何配置
	 * @return
	 * @author julong
	 * @date 2017-4-6 上午11:30:47
	 */
	@RequestMapping(value = "/get.json", method = RequestMethod.GET)
	@ResponseBody
	public String getPet() {
	    // implementation omitted
		return "123";
	}
	
	@RequestMapping(value = "/get0.json", method = RequestMethod.GET)
	@ResponseBody
	public String getPet0() throws JsonProcessingException {
	    // implementation omitted
		Map<String,String> maps = new Hashtable<String, String>();
		maps.put("1", "qwe");
		maps.put("2", "qwe");
		maps.put("3", "qwe");
		maps.put("4", "qwe");
		return new ObjectMapper().writeValueAsString(maps);
	}
	
	@RequestMapping(value = "/get1.json", method = RequestMethod.GET)
	@ResponseBody
	public Map<String,String> getPet1() {
	    // implementation omitted
		Map<String,String> maps = new Hashtable<String, String>();
		maps.put("1", "qwe");
		maps.put("2", "qwe");
		maps.put("3", "qwe");
		maps.put("4", "qwe");
		return maps;
	}
	
	@RequestMapping(value = "/get2", method = RequestMethod.GET)
	public String getPet2() {
	    // implementation omitted
		Map<String,String> maps = new Hashtable<String, String>();
		maps.put("1", "qwe");
		maps.put("2", "qwe");
		maps.put("3", "qwe");
		maps.put("4", "qwe");
		return "index";
	}
	
}

 

本项目采用的是 maven  需要的jar如下

<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.2.0.RELEASE</version>
			<scope>runtime</scope>
			<exclusions>
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>3.2.0.RELEASE</version>
			<scope>runtime</scope>
		</dependency>


		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>3.2.0.RELEASE</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>3.2.0.RELEASE</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>3.2.0.RELEASE</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>3.2.0.RELEASE</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>3.2.0.RELEASE</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>3.2.0.RELEASE</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.2.0.RELEASE</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.2.0.RELEASE</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc-portlet</artifactId>
			<version>3.2.0.RELEASE</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>3.2.0.RELEASE</version>
			<scope>runtime</scope>
		</dependency>
		<!-- json -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.2.0</version>
		</dependency>

		<!-- logger -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>1.5.8</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.5.8</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.5.8</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.14</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.openejb</groupId>
			<artifactId>javaee-api</artifactId>
			<version>5.0-1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.faces</groupId>
			<artifactId>jsf-api</artifactId>
			<version>1.2_04</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.faces</groupId>
			<artifactId>jsf-impl</artifactId>
			<version>1.2_04</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>

 

界面测试如下:

http://localhost:8080/SpringJson/get.json 直接返回的string 这种方式 是不用处理的 

123

http://localhost:8080/SpringJson/get0.json 这种方式 是在代码里做了 json转换没有用spring的方式

{"4":"qwe","3":"qwe","2":"qwe","1":"qwe"}

http://localhost:8080/SpringJson/get1.json  这种方式使用的是spring的配置 再看官方的文档中 有说明 

{"4":"qwe","3":"qwe","2":"qwe","1":"qwe"}


说明如下:只要有这个 驱动 下边的这些都可以使用

    This is the complete list of HttpMessageConverters set up by mvc:annotation-driven:

    ByteArrayHttpMessageConverter converts byte arrays.

    StringHttpMessageConverter converts strings.

    ResourceHttpMessageConverter converts to/from org.springframework.core.io.Resource for all media types.

    SourceHttpMessageConverter converts to/from a javax.xml.transform.Source.

    FormHttpMessageConverter converts form data to/from a MultiValueMap<String, String>.

    Jaxb2RootElementHttpMessageConverter converts Java objects to/from XML — added if JAXB2 is present on the classpath.

    MappingJackson2HttpMessageConverter (or MappingJacksonHttpMessageConverter) converts to/from JSON — added if Jackson 2 (or Jackson) is present on the classpath.

    AtomFeedHttpMessageConverter converts Atom feeds — added if Rome is present on the classpath.

    RssChannelHttpMessageConverter converts RSS feeds — added if Rome is present on the classpath.

这种方式免去了复杂的配置直接一句话就能搞定json问题,特此一记希望对您有所帮助


 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页