I. Overview
JSON strings in different formats to be implemented in the background this is one of the things we often do, this blog will introduce you to four different JSON strings to the background how to use @requestbody to parse these different formats of JSON strings.
second, the Code show
Jar packages that need to be referenced
1.xml Configuration
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 "><servlet><servlet-name>springmvc</ Servlet-name><servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name> springmvc</servlet-name><url-pattern>*.spring</url-pattern></servlet-mapping>< Welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app >
Springmvc-servlet.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Mvc= "Http://www.springframework.org/schema/mvc" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= " http://www.springframework.org/schema/p "xmlns:context=" Http://www.springframework.org/schema/context "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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd "><context:component-scan base-package=" Com.gaowei.JSON "/><mvc:annotation-driven/></beans>
2.java Code
Userinfo.java
Package Com.gaowei.entity;public class Userinfo {private string username;private string Password;public string GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;}}
Test.java
Package Com.gaowei.json;import Java.util.arraylist;import Java.util.list;import java.util.map;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestbody;import Org.springframework.web.bind.annotation.requestmapping;import Com.gaowei.entity.Userinfo; @Controllerpublic class Test {@RequestMapping (value= "getJSON1") public void getJSON1 (@RequestBody Userinfo Userinfo) {System.out.println ("--- ---getJSON1---start----"); System.out.println (Userinfo.getusername ()); System.out.println (Userinfo.getpassword ()); SYSTEM.OUT.PRINTLN ("------getJSON1---end----");} @RequestMapping (value= "getJSON2") public void getJSON2 (@RequestBody arraylist<string> list) { SYSTEM.OUT.PRINTLN ("------getJSON2---start----"); for (int i = 0; i < list.size (); i++) {System.out.println (List.get ( i));} SYSTEM.OUT.PRINTLN ("------getJSON2---end----");} @RequestMapping (value= "getJSON3") public void getJSON3 (@RequestBody list<map> List) {System.out.println ("----- -getjson3---start----"); for (int i = 0; i < list.size (); i++) {Map map=list.get (i); System.out.println (Map.get ("username") + "" +map.get ("password")); SYSTEM.OUT.PRINTLN ("------getJSON3---end----");} @RequestMapping (value= "getJSON4") public void getJSON4 (@RequestBody map map) {System.out.println ("------ GETJSON4---start----"); System.out.println (Map.get ("username")); List<map> worklist= (List) map.get ("work"), for (int i = 0; i < worklist.size (); i++) {Map eachaddressmap=worklist. Get (i); System.out.println ("address=" +eachaddressmap.get ("Address");} Map schoolmap= (map) map.get ("school"); System.out.println (Schoolmap.get ("name")); System.out.println (Schoolmap.get ("Address")); SYSTEM.OUT.PRINTLN ("------getJSON4---end----");}}
3. Interface code
test.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
4.
Third, summary.
It is important to note that the JAR package reference does not refer to all of Spring's jar packages as errors that would cause a jar packet conflict to be reported to HTTP 415. @RequestBody This is a powerful way to convert JSON strings into entities, arrylist, maps, and so on. This method has greatly improved the development efficiency of our developers.
Novice Learning Spring--springmvc Annotated version parsing JSON strings in different formats