利用SpringMvc和jackson-databind来实现请求和响应的json mapping
新建一个maven web工程,工程结构如下,工程模拟一个简单的用户资料获取服务,请求和响应都是基于json来做
web.xml设置
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/lzw/mapping/mapping-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
mapping-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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-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/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"> <context:component-scan base-package="com.lzw.mapping.service"/> <mvc:annotation-driven/> </beans>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lzw</groupId> <artifactId>json-mapping</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>json-mapping Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.10.RELEASE</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.3.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>json-mapping</finalName> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>7.1.6.v20100715</version> <configuration> <scanIntervalSeconds>2</scanIntervalSeconds> <webAppConfig> <contextPath>/</contextPath> <defaultsDescriptor>${project.basedir}/src/main/resources/webdefault.xml</defaultsDescriptor> </webAppConfig> </configuration> </plugin> </plugins> </build> </project>
com.lzw.mapping.beans 下放置的是用户的领域模型,CustomerBasicInfo是请求的模型,Customer是响应的模型
package com.lzw.mapping.beans; public class Customer { private String id; private String name; private String address; private String gender; private String phone; ...省略getter和setter }
package com.lzw.mapping.beans; public class CustomerBasicInfo { private String id; private String name; ...省略getter和setter }
CustomerInfoService
package com.lzw.mapping.service; import com.lzw.mapping.beans.Customer; import com.lzw.mapping.beans.CustomerBasicInfo; public interface CustomerInfoService { public Customer getCustomer(CustomerBasicInfo customerInfo); }
package com.lzw.mapping.service.impl; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.lzw.mapping.beans.Customer; import com.lzw.mapping.beans.CustomerBasicInfo; import com.lzw.mapping.service.CustomerInfoService; @Controller @RequestMapping(value="/customerservice") public class CustomerInfoServiceImpl implements CustomerInfoService { @RequestMapping(value="/getcustom",method=RequestMethod.POST) @ResponseBody public Customer getCustomer(@RequestBody CustomerBasicInfo customerInfo) { System.out.println("Customer query info:" + "id:" + customerInfo.getId() + " name:" + customerInfo.getName()); Customer customer = new Customer(); customer.setId(customerInfo.getId()); customer.setGender("male"); customer.setName("lzw"); customer.setPhone("88888888"); customer.setAddress("GZ"); return customer; } }
通过httprequester来验证请求
可以看到请求和结果都实现了json和pojo的mapping
代码下载