首先,演示一下出现的情况,创建一个实体类,代码如下:

public class User2 {private Long id;private String username;public User2() {}public User2(Long id, String username) {this.id = id;this.username = username;}
}

使用spring mvc的代码,向前端返回这个类的json格式数据:

@RestController
@RequestMapping("/user")
public class UserController {@GetMapping("getuser")public User2 getUser() {return new User2(1L, "jack");}}

访问结果:

可以看到,报了一个异常:

org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class com.learn.entity.User2

报这个异常与jackson这个库的序列化原理有关,和fastjson一样,jackson在序列化的时候,先利用反射找到对象类的所有get方法,获取方法名称,然后去掉前面的“get”,把剩下的字符串首字母小写,作为json的每个key值,而get方法的返回值作为value。接下来再反射field,添加到json中。

所以,给User2实体类添加好getter方法就行了。不过平时开发中,一般实体类的getter、setter方法都是会使用插件或者自己快速生成,正常情况下是不会出现这个问题的。

//@Data
public class User2 {private Long id;private String username;public User2() {}public User2(Long id, String username) {this.id = id;this.username = username;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}
}

HttpMessageNotWritableException: No converter found for return value of type,可能出现的原因及解决方法相关推荐

  1. Spring Boot:关于“No converter found for return value of type: class xxx”的解决方法

    Spring Boot:关于"No converter found for return value of type: class xxx"的解决方法 参考文章: (1)Sprin ...

  2. 【Spring】HttpMessageNotWritableException No converter found for return value of type

    1.背景 Spring boot 启动报错 Failed to write HTTP message: org.springframework.http.converter.HttpMessageNo ...

  3. SpringBoot:HttpMessageNotWritableException: No converter found for return value of type

    网上搜了一圈,总结一下出现上述问题的主要原因: 未写报错的类相应属性的:setter.getter 缺少jackson包支撑:类对象 -> Json数据格式的转换 对于1中出现的问题,请仔细检查 ...

  4. JavaWeb项目中出现No converter found for return value of type的解决方法

    目录 异常展示: 解决方法 原理: 拓展 异常展示: 意思就是找不到返回值类型的转换器,其实就是你的controller在进行json数据返回的时候,找不到进行json数据转换的转换器.我这里使用的是 ...

  5. SpringBoot 封装返回类报错:No converter found for return value of type

    封装完毕Result返回类,使用PostMan测试时报错 {"timestamp": "2020-06-03T10:38:03.144+0000"," ...

  6. java统一返回结果集封装,解决No converter found for return value of type

    网上很多,自己参照手写了一个. package com.***.pro.utils;/*** @Author: wsh*/public class Result<T> {private B ...

  7. No converter found for return value of type: class xxx(自定义的class对象)

    先贴一份异常信息: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for ...

  8. No converter found for return value of type错误解决以及消息转化器简单分析

    一.错误发生的背景 最近搭建一个springboot项目,在开发到全局异常处理的时候,运行报错.具体代码以及错误信息如下: 代码:GlobalExceptionHandler(异常处理类) /*** ...

  9. Spring MVC No converter found for return value of type

    严重: Servlet.service() for servlet [xxx] in context with path [] threw exception [Request processing ...

最新文章

  1. 免费在线共享思维导图敏捷开发工具Leangoo
  2. EXE和SYS通信(ReadFile WriteFile) 其他方式
  3. 累加求和a+aa+aaa+aa…a_过半深圳券商评级跃升4家维持在AA级
  4. Javascript归并排序
  5. 【分享用于学习C++图像处理的代码示例】框架
  6. 零基础学习嵌入式给出的10条中肯的建议
  7. 【图像分割】基于matalb GUI遗传神经网络图像分割【含Matlab源码 659期】
  8. vs2013编译驱动错误-Inf2Cat,signability test failed
  9. CS之攻击菜单详解-后门生成与上线
  10. [AE] ArcGIS Engine处理Landsat8数据
  11. Excel 的进阶学习
  12. 手机/iPad异地远程桌面控制Windows电脑【无公网IP】
  13. 用react-custom-scrollbars插件美化 Ant Design Table 滚动条
  14. 在一个局域网中,共享的项目别人访问不了,解决方法
  15. 【压缩包打不开的原因和解决方法】
  16. 治疗骨髓增殖性肿瘤的一种新型高选择性酪氨酸激酶抑制剂ZT55的研究
  17. stm32h7 串口idle_【STM32H7教程】第30章 STM32H7的USART应用之八个串口FIFO实现
  18. 跳槽、换房、不忘输出,与你分享我匆忙的 2022~
  19. 视频剪辑混剪教程,AI智剪所有视频素材
  20. 秒杀ibluever 免越狱iPad wifi版连接蓝牙“双环”直接上网

热门文章

  1. android app hilink,HUAWEI HiLink
  2. 计算机程序c语言教材,全国计算机等级考试二级C语言程序设计教材(2018年版)...
  3. 智能穿戴的未来与机遇:打造“穿戴的智能化”
  4. IDE是什么?它和文本编辑器相比有什么优缺点?
  5. 使用switchHost管理host
  6. Streamlit学习使用(一)
  7. vue+express+websocket+mongodb在线群聊工具
  8. poscms基础问题汇总
  9. 高校实验室综合管理系统:建立一个安全、智慧、规范的实验室
  10. 复杂的前端登录页面代码