Spring MVC--接收JSON格式的数据

Spring mvc使用开源的Jackson包来处理JSON格式的请求或相应消息。

index.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




测试接收JSON格式的数据





编号:
书名:
作者:

BookController.java

package org.zhangxueliang.controller;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.zhangxueliang.domain.Book;

@Controller
@RequestMapping("/json")
public class BookController {
	
	private static final Log logger = LogFactory.getLog(BookController.class);
	
	// @RequestBody根据json数据,转换成对应的Object
    @RequestMapping(value="/testRequestBody")
    public void setJson(@RequestBody Book book,
    		HttpServletResponse response) throws Exception{
    	// ObjectMapper类是Jackson库的主要类。它提供一些功能将Java对象转换成对应的JSON格式的数据
    	ObjectMapper mapper = new ObjectMapper();
    	// 将book对象转换成json输出
    	logger.info(mapper.writeValueAsString(book) );
    	book.setAuthor("张学亮");
    	response.setContentType("text/html;charset=UTF-8");
    	// 将book对象转换成json写出到客户端
    	response.getWriter().println(mapper.writeValueAsString(book));
    }

}


Book.java


package org.zhangxueliang.domain;

import java.io.Serializable;
public class Book implements Serializable {

	private Integer id;
	private String name;
	private String author;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	@Override
	public String toString() {
		return "Book [id=" + id + ", name=" + name + ", author=" + author + "]";
	}

}

springmvc-config.xml



<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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd     
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd">
        
    
    <context:component-scan base-package="org.zhangxueliang.controller"/>
	
	<mvc:annotation-driven/>
	
    <mvc:default-servlet-handler/>
	

    
     <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        
        <property name="prefix">
            <value>/WEB-INF/content/value>
        property>
        
        <property name="suffix">
            <value>.jspvalue>
        property>
    bean>
    
beans>

web.xml



<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
	http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
	id="WebApp_ID" version="3.1">
	
  <servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    servlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>/WEB-INF/springmvc-config.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>
  
  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
  
  
  <filter>
		<filter-name>characterEncodingFilterfilter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
		<init-param>
			<param-name>encodingparam-name>
			<param-value>UTF-8param-value>
		init-param>
 filter>
	<filter-mapping>
		<filter-name>characterEncodingFilterfilter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>
	
web-app>

你可能感兴趣的:(SSM)