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.