zoukankan      html  css  js  c++  java
  • SpringMVC注解@RequestMapping之produces属性导致的406错误

    废话不多说,各位,直接看图说话,敢吗?这个问题网上解决的办法写的狠是粗糙,甚至说这次我干掉它完全是靠巧合,但是也不否认网上针对406错误给出的解决方式,可能是多种情况下出现的406吧?我这次的流程就是集成了MyBatis的分页插件,简单实现了一个分页功能,最后将数据返回给浏览器,就因为我的随手一粘贴,才引发了这场406,别忘了各位看图说话。

           我首先说说我的惨痛经历吧,一直对spring框架的AOP原理狠是模糊,就在上周五自己想好好研究一下,但是很多美好的事情都不是那么一路绿灯,磕磕绊绊总算是差不多了,但是在最后给我返回一个406错误,这可是我第一次碰到这种傻逼错误,真的,对于我这个2年的猿来说确实有点手忙错乱,最后我靠着网上的一些指点凭直觉把它干掉了,在这里给分为分享一下。

           在使用SpringMVC的@RequestMapping注解时注意,如果你为了解决http请求响应的乱码问题,执意在@RequestMapping注解上使用produces属性来快速解决,那么这时候你可要注意了,假如你给浏览器返回一个对象并且使用@ResponseBody注解自动转成json数据返回的同时并且使用了produces属性来解决乱码问题,那么情况就不是太理想了,406随之而来。

    1 @RequestMapping(value="/itemsPage",method=RequestMethod.GET,produces = "text/plain;charset=UTF-8")
    2 @ResponseBody
    3 public List<Item> itemsPage(String currentPage,String pageSize){
    4    return itemService.findItemByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
    5 }

           这种错误我理解还不是狠透彻,但是我暂时把解决方式给各位分享一下,如果后期能得到高人的指点,会继续完善这篇博文,解决这种问题的方式有俩种:一是直接将produces属性去掉,万一出现了乱码则在配置文件或编解码的方式处理,二是先将对象转成json数据,然后可以使用produces属性解决乱码问题。

    解决方式一:

    1 @RequestMapping(value="/itemsPage",method=RequestMethod.GET)
    2 @ResponseBody
    3 public List<Item> itemsPage(String currentPage,String pageSize){
    4     return itemService.findItemByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
    5 }

    解决方式二:

    复制代码
    1 @RequestMapping(value="/itemsPage",method=RequestMethod.GET,produces = "text/plain;charset=UTF-8")
    2 @ResponseBody
    3 public String itemsPage(String currentPage,String pageSize){
    4     List<Item> PageItems = itemService.findItemByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
    5     return JsonUtil.object2Json(PageItems);
    6 }
    复制代码

     转载于:https://www.cnblogs.com/1315925303zxz/p/7404299.html

  • 相关阅读:
    CSS中position小解
    position
    mac默认安装postgresql, 如何让postgresql可以远程访问
    The data directory was initialized by PostgreSQL version 9.6, which is not compatible with this version 10.0.
    active admin gem error
    psql 无法添加超级用户
    ubuntu 15.04 安装Balsamiq Mockups 3
    Rails html 写public里图片的路径
    rails c 历史命令
    undefined local variable or method `per' for []:ActiveRecord::Relation
  • 原文地址:https://www.cnblogs.com/it-deepinmind/p/11804476.html
Copyright © 2011-2022 走看看