SpringMVC 中使用 @ResponseBody 返回Json時,須要手動添加jackson依賴

No converter found for return value of type: class java.util.HashMap
SpringMVC 中使用 @ResponseBody 返回Json時,須要手動添加jackson依賴!Maven添加:html

<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</artifactId> <version>2.4.3</version> </dependency>

``前端

不用maven也能夠,springMVC-servlet.xml 配置以下java

<!-- 啓動Springmvc註解驅動 --> <mvc:annotation-driven/> <!-- 返回json 方法一 須要導入 fastjson.jar包 --> <mvc:annotation-driven> <mvc:message-converters register-defaults="false"> <!-- 避免IE執行AJAX時,返回JSON出現下載文件 --> <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <!-- 這裏順序不能反,必定先寫text/html,否則ie下出現下載提示 --> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 返回json 方法二 須要導入 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") @ResponseBody public Object getJson(){ Map<String, Object> map=new HashMap<String, Object>(); map.put("fd", "郝鵬"); return map; }

能夠返回map到前端了,如前端則ajax請求,返回數據如data.fd爲郝鵬web

 
同時能夠參考:https://www.cnblogs.com/gxz-sw/p/6860447.html
相關文章
相關標籤/搜索