When using @ResponseBody to return JSON in SPRINGMVC, you need to manually add the Jackson dependency

Source: Internet
Author: User

No Converter found for return value of Type:class Java.util.HashMap
When using @ResponseBody to return JSON in SPRINGMVC, you need to manually add the Jackson dependency! Maven Add:

<Dependency><Groupid>com.fasterxml.jackson.core</Groupid><Artifactid>jackson-core</artifactid> < version>2.4.3</version></dependency>< dependency> <groupId> Com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</< Span class= "Hljs-name" >artifactid> <version>2.4.3</version></ Dependency>              

``

You can do it without MAVEN, Springmvc-servlet.xml is configured as follows

<!--start Springmvc annotation Driver<Mvc:annotation-driven/><!--return JSON method one needs to import Fastjson.jar package--<Mvc:annotation-driven><Mvc:message-convertersregister-defaults="False" ><!--avoid IE when performing Ajax, return JSON appears download file--<BeanId="Fastjsonhttpmessageconverter"class="Com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter" ><PropertyName="Supportedmediatypes" ><List><!--here the order can not be reversed, must first write text/html, or IE under the download prompt<Value>text/html;charset=utf-8</Value><Value>application/json;charset=utf-8</Value></List></Property></Bean></Mvc:message-converters></Mvc:annotation-driven><!--return JSON method two need to import jackson-annotations.jar,jackson-core.jar,jackson-databind.jar--><!--<bean class= " Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping "/> <bean class=" Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter "> <property name=" Messageconverters "> <list> <bean class=" Org.springframework.http.converter.StringHttpMessageConverter "> <property name=" supportedmediatypes "> <list> <value>text/html; charset=utf-8</value> <value>application/json;charset=UTF-8</value> </list> </property > </bean> <bean class= "Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" > <property name= "supportedmediatypes" > <list> <value>text/html; charset=utf-8</value> <value>application/json;charset=UTF-8</value> </list> </property > </bean> </list> </property> </bean>--> 
controller代码:@RequestMapping(value="/json")@ResponseBodypublic Object getJson(){    Map<String, Object> map=new HashMap<String, Object>(); map.put("fd", "郝鹏"); return map;}

You can return the map to the front end, such as the AJAX request on the front, and return data such as DATA.FD to Shangpeng

When using @ResponseBody to return JSON in SPRINGMVC, you need to manually add the Jackson dependency

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.