The basic of common Springmvc know, @RestController and @responsebody Plus will return JSON data. Their difference is mainly in the annotation aspect, one is the class level one is the method level.
Before we prefer to use @responsebody, later felt too much repetition, especially after the use of swagger, a method above the focus on the solution on the 4 to 5 layers, in order to reduce these complex process, we take a concise strategy, because are based on the front and back end of the separation of development, The front-end display also does not intend to use template languages such as JSP, Freemarke, volocity, etc. The direct use of HTML, of course, is mainly in the form of Ajax interaction.
How many cases are listed first? Typically, the class has a corresponding restcontroller annotation on it or a @controller annotation on your class, plus @responsebody on each method. Normally, the JSON data should be returned. Pass postman test or JMeter.
About @restcontroller and @responsebody source parsing can refer to my article: front-end interaction packaging AJAX+SPRINGMVC Source Analysis
Of course there are times when you do not return, depending on your use, such as using Fastjson but did not return the corresponding JSON data, can be resolved by the SPRINGMVC configuration file, add the following:
<!--Fastjson-- <mvc:annotation-driven> <mvc:message-converters register-defaults= " True "> <!--avoid IE when performing Ajax, return JSON appears download file-- <!--Fastjson--- <bean id=" Fastjsonhttpmessageconverter " class=" Com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter "> <property name= "Supportedmediatypes" > <list> <!--Here the order can not be reversed, must first write text/html, or the download prompt under IE, <value>text/html;charset=UTF-8</value> <value>application/json;charset=utf-8 </value> </list> </property> </bean> </mvc:message-converters > </mvc:annotation-driven>
If it is jackjson, it can be resolved by adding the following in the SPRINGMVC configuration file:
<mvc:annotation-driven> <mvc:message-converters> <!--avoid direct parsing of string types into json--> Class= "Org.springframework.http.converter.StringHttpMessageConverter"/> class= " Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter "/> </mvc: Message-converters> </mvc:annotation-driven>
Of course, if this is the case, it is usually possible to set the return value to Jsonobject or object instead of string, because the Jsonobject.tostring () method is also required to be converted by using string. Sometimes transitions don't work.
Of course, even this can be converted to JSON, such as using JQuery's Ajax method of interaction, can be escaped through eval or json.parse, the ordinary string into a JSON format string. In addition, there is a simple explanation, that is, if it is to return the Chinese garbled problem, pay attention to the corresponding method to add such a sentence produces= "Application/json;charset=utf-8", you can solve the garbled problem
If it's Android, remember that once I encountered this situation, but also to provide him with an interface, the sudden return is a string with a slash. He was replaced by the Replace method. However, personal advice, if you encounter this situation, you can use the return value of Jsonobject to avoid this situation.
About SPRINGMVC return data with slash string problem solution