【Json与Ajax交互报错解决】No converter found for return value of type: class com.github.pagehelper.PageInfo

简介: 【Json与Ajax交互报错解决】No converter found for return value of type: class com.github.pagehelper.PageInfo

 

Hello。你好呀,我是灰小猿,一个超会写bug的程序猿!

我今天又来修BUG了!!

image.gif编辑

事情是这样的:今天在整合SSM,读取数据库数据返回JSON字符串,并传递给前端界面Ajax进行交互的时候,向后台发送请求想要获取到返回的json数据,但是却出现了这样的错误,页面显示500:

image.gif编辑

HTTP Status 500 - org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class com.github.pagehelper.PageInfo

我翻译了一下大概意思就是:

请求处理失败; 嵌套异常是java.lang.IllegalArgumentException:没有找到转换器的类型返回值:类com.github.pagehelper.PageInfo  

然而我的能够从数据库中读取到数据并且返回JSON字符串的代码是这样的:

/**
   * 返回的数据携带json字符串 需要导入jackson包 
   * @param pn
   * @param model
   * @return
   */
  @RequestMapping("/emps")
  @ResponseBody
  public PageInfo getEmpsWithJson(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
    // 传入需要跳转的页面以及展示的数据量
    PageHelper.startPage(pn, 5);
    // 获取到全部数据
    List<Employee> employees = employeeService.getAll();
    System.out.println("查询到的数据:");
    System.out.println(employees);
    // 使用pageinfo包装查询的数据,只需要将pageinfo交给页面即可
    // 传入数据,以及要显示多少页
    PageInfo page = new PageInfo(employees, 5);
    System.out.println("封装数据:");
//    System.out.println(page);
    return page;
  }

image.gif

我这里使用了@responseBody注解,该注解的作用就是会自动将返回数据封装成json格式,但是现在我的前台接收不到封装的json对象,查到的数据也无法封装成JSON对象,

最开始我找不到问题的解决,在网上找到了很多类似的错误,但是他们回答都模棱两可,并不能实际的解决问题,难道说程序员寻找bug的救命稻草的时候都是这样的嘛?

image.gif编辑

网上的解决基本上都是说导入json的依赖,但是现在使用Maven做项目的时候,在pom.xml中引入json的依赖坐标,一般都会自动生成相应的三个jar包,分别是

    • jackson-databind-x.x.x.jar
    • jackson-annotations-x.x.x.jar
    • jackson-core-x.x.x.jar

    小伙伴们看一下,如果说哪个小伙伴发现确实是缺少这三个jar中的其中一个,那么你去导入一下就行了,导入的时候可以到如下的maven仓库导入:

    https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.7.4

    但是现在基本上都不会说是缺少上面的哪个jar包了,

    所以如果你并没有缺少jar包,那么应该咱们的问题就一样了!

    解决方案:

    对于No converter found for return value of type错误,原因一般都是jar包版本冲突的问题,和它冲突的jar包是spring的相关jar包,具体为什么冲突我也不知道........

    image.gif编辑

    我的spring的jar包的版本是4.3.7的,然后json的版本使用2.8.8的运行会报上述错误,

    所以解决就是更换其他版本的json的jar包,我建议的是更换低版本的,我更换了2.7.4的jar包,然后再运行就可以了,如果你的不行,你可以再换个其他版本的jar包试试,

    在pom.xml中导入json的jar包的依赖代码是:

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.7.4</version>
    </dependency>

    image.gif

    接着重新启动tomcat,再运行就正确了!

    image.gif编辑

    如果大家还有其他解决方法,欢迎留言提出!

    我是灰小猿,我们下期见!

    image.gif编辑

    目录
    相关文章
    |
    5天前
    |
    JSON 前端开发 JavaScript
    JSON parse error: Cannot deserialize value of type `java.lang.Integer` from Boolean value
    这篇文章讨论了前端Vue应用向后端Spring Boot服务传输数据时发生的类型不匹配问题,即后端期望接收的字段类型为`int`,而前端实际传输的类型为`Boolean`,导致无法反序列化的问题,并提供了问题的诊断和解决方案。
    JSON parse error: Cannot deserialize value of type `java.lang.Integer` from Boolean value
    |
    17天前
    |
    JSON 数据格式 Python
    【python】解决json.dump(字典)时报错Object of type ‘float32‘ is not JSON serializable
    在使用json.dump时遇到的“Object of type ‘float32’ is not JSON serializable”错误的方法,通过自定义一个JSON编码器类来处理NumPy类型的数据。
    19 1
    |
    1月前
    |
    网络安全 开发工具 git
    【git】解决git报错:ssh:connect to host github.com port 22: Connection timed out 亲测有效
    【git】解决git报错:ssh:connect to host github.com port 22: Connection timed out 亲测有效
    168 1
    |
    22天前
    |
    机器学习/深度学习 人工智能 分布式计算
    人工智能平台PAI操作报错合集之引用github.com/alibaba/pairec包时报错,该如何解决
    阿里云人工智能平台PAI是一个功能强大、易于使用的AI开发平台,旨在降低AI开发门槛,加速创新,助力企业和开发者高效构建、部署和管理人工智能应用。其中包含了一系列相互协同的产品与服务,共同构成一个完整的人工智能开发与应用生态系统。以下是对PAI产品使用合集的概述,涵盖数据处理、模型开发、训练加速、模型部署及管理等多个环节。
    |
    1月前
    |
    网络安全 开发工具 git
    使用git克隆仓库报错:Warning: Permanently added‘github.com’ to the .....(ssh )
    所以,你可以安全地忽略这个警告,它不会影响到你使用git克隆仓库。如果你已经成功克隆了仓库,那么一切都在正常工作。如果你在克隆过程中遇到其他问题,那可能需要查看具体的错误信息来解决。
    21 0
    |
    2月前
    【完美解决】Github action报错remote: Write access to repository not granted.
    【完美解决】Github action报错remote: Write access to repository not granted.
    291 1
    |
    1月前
    |
    JSON 前端开发 数据格式
    【Python】已解决:TypeError: Object of type JpegImageFile is not JSON serializable
    【Python】已解决:TypeError: Object of type JpegImageFile is not JSON serializable
    37 0
    |
    1月前
    |
    JSON 数据格式
    Unsupported Media Type,传入的字符串数据:这里应该是Json
    Unsupported Media Type,传入的字符串数据:这里应该是Json
    |
    1月前
    |
    JSON 数据格式
    Content type ‘text/plain;charset=UTF-8‘ not supported,这里要把测试文件转为json格式
    Content type ‘text/plain;charset=UTF-8‘ not supported,这里要把测试文件转为json格式
    Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension.
    Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension.