zoukankan      html  css  js  c++  java
  • SpringMVC 中使用 @ResponseBody 返回Json时,需要手动添加jackson依赖

    No converter found for return value of type: class java.util.HashMap
    SpringMVC 中使用 @ResponseBody 返回Json时,需要手动添加jackson依赖!Maven添加:

    <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 配置如下

    <!-- 启动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为郝鹏

     
    同时可以参考:https://www.cnblogs.com/gxz-sw/p/6860447.html
     
     
  • 相关阅读:
    echarts labelLayout
    可视化学习及实战记录
    VS2008提示无法打开包括文件:“afxcontrolbars.h”解决办法
    原码、补码和反码
    第一篇
    vc2008编译就提示找不到msvcr90d.dll
    Vue H5 与 APP 交互 (IOS为例)
    VS Code中小程序与Vue常用插件合集(前端合集)
    如何在Element 使用正则表达式校验
    分享CSS公共类库(能在项目快捷使用CSS类)
  • 原文地址:https://www.cnblogs.com/tongxinling/p/7862198.html
Copyright © 2011-2022 走看看