SpringMVC receives Post object/JSon data, springmvcjson

Source: Internet
Author: User

SpringMVC receives Post object/JSon data, springmvcjson

Interface code:

@ ResponseBody
@ RequestMapping (value = "/test", method = RequestMethod. POST)/* only allow the POST method to call this interface */
Public returnType functionName (/* POST data content */@ RequestBody parameterType parameterName, HttpServletRequest request) throws Exception {}

 

Configuration:

<Bean id = "multipartResolver" class = "org. springframework. web. multipart. commons. CommonsMultipartResolver">
</Bean>

 

Dependent files:

<Dependency>
<GroupId> commons-io </groupId>
<ArtifactId> commons-io </artifactId>
<Version> 2.5 </version>
</Dependency>
<Dependency>
<GroupId> commons-fileupload </groupId>
<ArtifactId> commons-fileupload </artifactId>
<Version> 1.2.1 </version>
</Dependency>

 

Logic code:

/* If the List data needs to be converted after receiving the data */

/* Otherwise, conversion is not required */

ObjectMapper mapper = new ObjectMapper ();

For (int I = 0; I <behaviorList. size (); I ++ ){
/* Because the List POST from the client is of the LinkedHashMap type
* Therefore, ObjectMapper must be used for parsing and conversion */

ClassName clazz = mapper. convertValue (List. get (I), ClassName. class );

}

 

Entity class:

If the access end is C # DateTime type, it must be reset to String type; otherwise, the server cannot parse

 

 

Access end (C #):

/* Before POST, convert the object class to a JSon string and then convert it to a Byte array */

HttpWebRequest httpReq = (HttpWebRequest) WebRequest. Create (uri );

HttpReq. Method = "POST ";
HttpReq. Accept = "*/*";
HttpReq. ContentType = "application/json; charset = UTF-8 ";

Byte [] buffer = Encoding. UTF8.GetBytes (dataJsonString );

HttpReq. ContentLength = buffer. Length;
HttpReq. GetRequestStream (). Write (buffer, 0, buffer. Length );

HttpWebResponse httpResp = (HttpWebResponse) httpReq. GetResponse ();
Stream respStream = httpResp. GetResponseStream ();
StreamReader respStreamReader = new StreamReader (respStream, Encoding. UTF8 );
String result = respStreamReader. ReadToEnd ();

 

 

The above is a solution that I tried to find in one day when I encountered a problem.

My java cainiao solution may be one-sided or outdated or clumsy.

I also hope to help my friends who encounter the same problem.

 

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.