json data interaction

1.1 @requestbody

Role:The @requestbody annotation is used to read the content (string) of the http request, and the read content is converted into data in the format of json, xml, etc. through the httpmessageconverter interface provided by springmvc and bound to the parameters of the controller method.

Application of this example:@requestbody annotation is implemented to receive json data of http request and convert json data to java object

1.2 @responsebody

Role:The annotation is used to return the object of the controller method,Data converted to the specified format through the httpmessageconverter interface, such as json, xml, etc., and sent to the client in response

This example application:@responsebody annotation implementation to convert the controller method return object into a json response to the client

1.3 request json, response json implementation:

1.3.1 Environmental preparation

Springmvc uses mappingjacksonhttpmessageconverter to convert json data by default.Need to add jackson's package, as follows:

1.3.2 Configure json converter

Add messageconverters to the annotation adapter

<!-Annotation Adapter->
<property name="messageconverters">

Note:If you use, you don't need to define the content above.

1.3.3 Controller

//Commodity modification submits json information and responds to json information
@requestmapping ("/edititemsubmit_requestjson")
public @responsebody items edititemsubmit_requestjson (@requestbody items items) throws exception {
system.out.println (items);
return items;

1.3.4 page js method writing:

Introducing js:

<script type="text/javascript"
src="${pagecontext.request.contextpath} /js/jquery-1.4.4.min.js"<</script>
//Request json response json
function request_json () {
$.ajax ({
type:"post",url:"${pagecontext.request.contextpath} /item/edititemsubmit_requestjson.action",contenttype:"application/json;charset=utf-8",data:"{" name ":" Test product "," price ":99.9}",success:function (data) {
alert (data);

1.4 form submission, response json implementation:

Form submission is the most common practice.There are usually two methods, post and get,The response json data is to facilitate client processing.The implementation is as follows:

1.4.1 Environmental preparation

Same as the first example

1.4.2 Controller

//Commodity modification submission,Submit ordinary form data,Response json
@requestmapping ("/edititemsubmit_responsejson")
public @responsebody items edititemsubmit_responsejson (items items) throws exception {
system.out.println (items);
return items;

1.4.3 page js method writing:

function formsubmit () {
var user="name=test product&price=99.9";
alert (user);
 $.ajax (
type:"post", //change to get here can also be executed normally
url:"${pagecontext.request.contextpath}/item/edititemsubmit_requestjson.action",//contenttype is not specified will default to:application/x-www-form-urlencoded
data:user,success:function (data) {
alert (data.name);

From the js code above,The definition of contenttype has been removed. The contenttype defaults to:application/x-www-form-urlencoded format.

1.4.4 jquery form plugin

For the second method above,You can use jquery's form plugin to submit the form to achieve ajax submission form, as follows:

Quoting js:

<script type="text/javascript"
src="${pagecontext.request.contextpath} /js/jquery-1.4.4.min.js"<</script>
<script type="text/javascript"
src="${pagecontext.request.contextpath} /js/jquery.form.min.js"<</script>

The js method is as follows:

function response_json () {
//form object
var formobj=$("#itemform");
//Perform ajax submission
formobj.ajaxsubmit ({
datatype:"json", //Set expected server to return json
success:function (responsetext) {
alert (responsetext);

1.4.5 Summary

The second method is commonly used in actual development,Request key/value data and respond to the json result, which is convenient for the client to parse the result.

  • Previous Summary of thinkphp verification code
  • Next Detailed explanation of setting default value and event of select usage in Angular2