SPRINGMVC @RequestBody receive JSON object string demo

Source: Internet
Author: User

Springmvc this @RequestBody used relatively little, today to see a bit, or is very convenient.

@RequestBody receive a JSON string similar to [{name: ' Test '}, {name: ' Zhang San '}].

Look at the page first:

<! DOCTYPE html>functionTest () {varsavedataary=[]; vardata1={"name": "Test"}; vardata2={"name": "Zhang San"};              Savedataary.push (DATA1);                     Savedataary.push (DATA2); $.ajax ({type:"POST", URL:"Http://localhost/test/student", DataType:"JSON", ContentType:"Application/json", Data:JSON.stringify (savedataary), success:function(data) {alert (data)}}); }</script>

Last look at the background code:

Importjava.util.List;ImportOrg.springframework.web.bind.annotation.RequestBody;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.ResponseBody;ImportOrg.springframework.web.bind.annotation.RestController, @RestController @requestmapping ("/test") Public classTestController {@ResponseBody @RequestMapping (value= "/student", method=requestmethod.post) PublicString Student (@RequestBody list<student>students) {         for(Student s:students) {System.out.println ("Student Name:" +s.getname ()); }        return"OK"; }}classstudent{PrivateString name;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }        }

To summarize, this argument is JSON string.

SPRINGMVC @RequestBody receive JSON object string demo

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.