• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

No converter for [class java.util.LinkedHashMap] with preset Content-Type ‘text/html;charset=UTF-8‘]

武飞扬头像
好锅煮好饭
帮助1

记录一下debug日常…

前端网页做了个bootstrap表格,使用的是服务器分页方式,服务器返回的数据需要满足以下JSON格式规范:

{
  "total": 20, //总共满足条件的数据数量,bootstrap table会根据这个值和你设置pageSize,自动计算出页码
  "rows": [
    {
      "Id": 1,
      "ProductName": "香蕉",
      "StockNum": "100"
    },
    {
      "Id": 2,
      "ProductName": "苹果",
      "StockNum": "200"
    },
    {
      "Id": 3,
      "ProductName": "车厘子",
      "StockNum": "2010"
    }
  ]
}
学新通

我根据自己的情况自定义了一个返回对象:

public class NodeMessListJsonBuild {

    private Integer total;

    private List<NodeMessList> rows;

	......
}

控制器:

	@ResponseBody
    @RequestMapping(value = "/user/getUpgradeFileLists")
    public UpgradeFileListJsonBuild getUpgradeFileLists(QueryTableParms parms, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        //搜索框功能
        //当查询条件中包含中文时,get请求默认会使用ISO-8859-1编码请求参数,在服务端需要对其解码
        
        ...中间省略...
        
        UpgradeFileListJsonBuild upgradeFileListJsonBuild=new UpgradeFileListJsonBuild(total,lists);
        System.out.println(upgradeFileListJsonBuild);
        return upgradeFileListJsonBuild;
    }

启动程序后出现的情况:
1.springMVC工程时,以上写法不报错;
2.springBoot工程时,不报错;
3.springBoot工程基础上,我增加了webSocket功能,然后出现异常:

java.lang.IllegalArgumentException: No 'javax.websocket.server.ServerContainer' ServletContext attribute. Are you running in a Servlet container that supports JSR-356?

按照网上的方法,修改了pom.xml文件:
原来的:

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

改成:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>

然后“java.lang.IllegalArgumentException: No ‘javax.websocket.server.ServerContainer’ ServletContext attribute. Are you running in a Servlet container that supports JSR-356?”问题解决。

但是又报了另一个异常:

No converter for [class java.util.LinkedHashMap] with preset Content-Type 'text/html;charset=UTF-8']

我寻思着没有替换掉tomcat容器的时候都是运行的好好的(为了验证我把pom.xml依赖换回原先的,发现这个错误又没有了,但是我得用websocket只能替换,所以还是得解决这个问题)

根据网上的说法:
1.缺失Jackson 依赖,如果有 spring-boot-starter-web依赖可排除该问题
2.get、set方法需要有
3.注解使用是否正确:需要用@RestController(@Controller @ResponseBody)

我检查了一下,我都正确使用了,那问题出现在哪里呢?

又查看资料发现:
@ResponseBody注解,表示返回的是字符串,而此时要把@RequestMapping中的produces的值改为application/json才不会报错,如果是text/html则表示跳转到某个页面,前端无法解析为JSON字符串。

根据这个说法,我把控制器注解修改成:

@ResponseBody
    @RequestMapping(value = "/user/getUpgradeFileLists",produces = {"application/json;charset=UTF-8"})
    public UpgradeFileListJsonBuild getUpgradeFileLists(QueryTableParms parms, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        //搜索框功能
        //当查询条件中包含中文时,get请求默认会使用ISO-8859-1编码请求参数,在服务端需要对其解码
        
		......中间过程省略

        UpgradeFileListJsonBuild upgradeFileListJsonBuild=new UpgradeFileListJsonBuild(total,lists);
        System.out.println(upgradeFileListJsonBuild);
        return upgradeFileListJsonBuild;
    }

运行程序发现还是报错。
然后发现我的控制器里写了不该写的语句:

		request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

把这两句注释掉后,一切正常。。。。。
(也不知道什么原因之前没引入undertow的时候,这两句话没注释也不影响,服啦!)

这篇好文章是转载于:编程之路

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 编程之路
  • 本文地址: /boutique/detail/tanhgfaekc
系列文章
更多 icon
同类精品
更多 icon
继续加载