Spring for cainiao Learning -- SpringMVC Annotation Version parses JSON strings of different formats, springmvcjson

Source: Internet
Author: User

Spring for cainiao Learning -- SpringMVC Annotation Version parses JSON strings of different formats, springmvcjson

I. Overview
JSON strings of different formats are uploaded to the backend to implement functions. This is a common task, this blog introduces how to use @ RequestBody to parse JSON strings of different formats after four different JSON strings are uploaded to the background.
Ii. Code display

Jar package to be referenced


1. xml configuration

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"><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.



Iii. Summary.

Note that references to jar packages do not reference all jar packages in Spring, which may cause jar package conflicts and cause HTTP 415 errors. @ RequestBody this method is very powerful and can convert JSON strings into object classes, ArryList, Map and other objects. This method greatly improves the development efficiency of our developers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.