@RequestBody application

First of all, the parameter that @RequestBody needs to receive is a stringified json, and JSON.stringify is used directly here.} It's very simple, you don't need to manually convert json and entity, as long as you can map it (that is, the field name corresponds to the key of json, and the value can match the data type), then you can directly convert it. How to define "can be mapped up"? If the key in the json can find the corresponding field in the entity, then it is "can be mapped", that is to say: the key in the json passed in from the front desk must exist in the entity, otherwise an error will be reported, similar to the following Error org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "id" (Class com.westsoft.kft.repairs.dto.DispatchesDTO), not marked as ignorable In fact, to use @RequestBody this annotation requires additional Configure a Bean, that is, messageConverter, a message converter that automatically helps us receive json and convert it into an object. Since I am using a version after Spring 3.1, configure it under RequestMappingHandlerAdapter <!-- Spring3.1 The annotation HandlerAdapter -- > <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter "> </bean> <

Of course, you can also simply configure it through <mvc:annotation-driven />, so I won't introduce too much here~

Finally, a few more points

1. If 1. The contentType is not specified as json/application when the front desk sends the request 2. The messageConverter is not configured in Spring, then error 415 will be reported, that is, the type does not support the former because of Content type 'application/x-www-form-urlencoded; charset=UTF-8' not supported and the latter is because Content type 'application/json;charset=UTF-8' not supported Second, since the json conversion used in Spring uses jackson, it is necessary to introduce the jackson package how to use springmvc The @requestbody returns json data first configure XXX_ servletxml

<!-- Integrate jackson to return a json format -->

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> </list> </property> </ bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> Then the annotation in the Action method can be @RequestMapping(value="/XXXXXX", method = RequestMethod.POST) @ResponseBody public return type XXXXXX(){ } In what situations is @responsebody generally used, its advantages and disadvantages? @responsebody indicates that the return result of this method is directly written into the HTTP response body, which is generally used when obtaining data asynchronously. After using @RequestMapping, the return value is usually parsed as a jump path. After adding @responsebody, the return result will not be parsed as Jump path, but write it directly into the HTTP response body. For example, asynchronously obtaining json data and adding @responsebody will directly return json data.

Third, if there is a time type in the transmitted data, you should pay special attention. For attributes in the format of 01/21/2000 00::00:00, errors will occur during conversion, and you need to deal with them when you use them. .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325311729&siteId=291194637