spring mvc不能将返回的map自动转换为json, 报406错误

1.在 spring mvc 配置文件 如:  dispatcher-servlet.xml 中加入:

 

<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.json.MappingJackson2HttpMessageConverter"/>

           </list>

       </property>

</bean>

 

<mvc:annotation-driven />

加入的类要放在<mvc:annotation-driven />之前。

 

2. 在pom 文件中加入相应的依赖

 

   

    <dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-core</artifactId>

    <version>2.5.2</version>

    </dependency>

   

    <dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-databind</artifactId>

    <version>2.4.4</version>

</dependency>

 

<dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-annotations</artifactId>

    <version>2.4.3</version>

</dependency>

 

3. 测试类

@RequestMapping(value="/firstTime",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)

    public Map<String, Object> firstTime(HttpServletRequest request){

       logger.debug("requestBody:{}" + request.getAttribute(VerificationService.ATTRIBUTE_KEY));

       UserGame userGame = new DataInRequest().getModel(request, UserGame.class);

       //更新任务时长和移动次数

       userGame.setFinishNumber(1L);                 //设置完成游戏数为1,在查看文档的时候回查看该数

       jdbcService.saveOrUpdate(userGame);

       //返回id

       Map<String, Object> resultMap = new RespondHead().getRespondHeadMap();

       resultMap.put("id", userGame.getId());

       return resultMap;

 

    }

 

返回的map 就会自动转换为json

转载于:https://my.oschina.net/u/193184/blog/912191

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值