Json字符串转对象,使用ObjectMapper方式报错:no String-argument constructor/factory method
今天实训,springboot接受vue axios传来的json字符串对象,想将其转换为Java POJO对象,使用ObjectMapper方式如下:
@RequestMapping(value = "/insertSong", method = RequestMethod.POST)public String insertSong(@RequestBody String songStr) throws JsonProcessingException {ObjectMapper mapper = new ObjectMapper();Song song = mapper.readValue(songStr, Song.class);int count = musicWebService.insertSong(song);return count != 0 ? "Success" : "FAIL";}
报错:
com.fasterxml.jackson.databind.exc.MismatchedInputException:
Cannot construct instance of com.hui.domain.Song
(although at least one Creator exists):
no String-argument constructor/factory method to deserialize from String value
(’{“mid”:"",“musicName”:“1”,“musicSinger”:“1”,“musicLyric”:"",“musicType”:"",“musicImg”:"",“musicUrlID”:“1”,“musicCreationTime”:"",“musicPlayTimes”:0,“musicScore”:10,“musicComment”:“评论”}’)
但是明明我的Song对象有全参构造和无参构造方法
解决:
在POJO对象里加入如下构造方法:
public Song(String json) throws JsonProcessingException {Song song = new ObjectMapper().readValue(json, Song.class);this.mid = song.getMid();this.musicName = song.getMusicName();this.musicSinger = song.getMusicSinger();this.musicLyric = song.getMusicLyric();this.musicType = song.getMusicType();this.musicImg = song.getMusicImg();this.musicUrlID = song.getMusicUrlID();this.musicCreationTime = song.getMusicCreationTime();this.musicPlayTimes = song.getMusicPlayTimes();this.musicScore = song.getMusicScore();this.musicComment = song.getMusicComment();}
参考:
https://blog.csdn.net/qq_30162239/article/details/86647164
Json字符串转对象,使用ObjectMapper方式报错:no String-argument constructor/factory method相关推荐
- JSON.parse 解析json字符串时,遇换行符报错
Json字符串转换成Json对象时候,有两种方式: 假设d是json字符串: 1,eval('(' + d + ')'). 2,JSON.parse(d): 但是以上方式有隐患,如果Json字符串有换 ...
- Jackson:数组json字符串转对象集合(List)的两种方式
首先,创建个实体类Person import java.util.List;public class Person {private String name;private Integer age;p ...
- json字符串转对象的几种方式
1. java自带的原生sf.json json字符串转对象 import net.sf.json.JSONObjectString response="{\"status\&qu ...
- Json字符串和对象相互转换
文章目录 1.JsonUtil 工具类 把对象转换为json字符串 把json字符串转换为对象 把json字符串转换为List集合 2.Gson 把对象转换为json字符串 把json字符串转换为对象 ...
- JSON字符串和对象之间的转换
使用jackSON来实现JSON字符串和对象之间的转换 引入maven <!--jackson--><dependency><groupId>com.fasterx ...
- json字符串和对象的相互转换
大家好呀,我是柚子,今天这篇文章介绍的是json字符串和对象的相互转换~ 文章目录 举例 一.json字符串转对象 1.单个对象 2.多个对象 二.对象转json字符串 1.第一种方式 2.第二种方式 ...
- json字符串转对象(json字符串转对象的方法)
js中如何将一字符串表示的日期转换为Date对象? var a = "2006/12/1" d = new Date(a) <br/>写个json对象demo瞧一瞧,j ...
- 开发:随笔记录之 Json字符串和对象的相互转换
引入的包 :json-lib-2.1.jar import net.sf.json.JSONArray; import net.sf.json.JSONObject; //随意创建一个实体 RuleD ...
- Json字符串转对象和转List集合操作(alibabab版本)
Json字符串转对象和转List集合操作(alibabab版本) Json是当前开发用得最多基于JavaScript语言的轻量级的数据交换格式,总结一下常用转换格式的方法,以便日后使用方便 以下为 a ...
最新文章
- nginx源码分析--内存对齐处理
- Xamarin.Android编译提示找不到mscorlib.dll.so文件
- 用python画四叶草代码-python—字符串拼接三种方法
- ASP.NET Core中使用表达式树创建URL
- 丢失模式不联网有用吗_用眼过度,打开手机“护眼模式”真的有用吗?
- 中医3个不花钱的养肾方
- int 和 Integer 的区别
- Java 练习:编写 Java 程序,输入年份和月份,使用 switch 结构计算对应月份的天数。月份为 1、3、5、7、8、10、12 时,天数为 31 天。月份为 4、6、9、11 时,天数为 3
- linux 中 man 1 man2 man3 ......man N的区别
- 均分纸牌模型之mxj分礼物
- 5G,如何为新基建按下“加速键”?
- 将控制台输出重定向到textbox的dotnet类
- Qt中mouseMoveEvent在MainWindow中使用
- ios13 微信提示音插件_ios13怎么改微信提示音
- 给ssh服务添加谷歌双重认证
- 使用大白菜装机维护版软件取消Win7开机密码
- 微博服务器瘫痪容易修复吗,微博服务器九次瘫痪,还有一个竟然连崩四回,程序员:放过我吧!...
- 怎样记账简单明白,实用记账技巧
- 基于springboot地方旅游系统的设计与实现
- 牛客Top101 JS合并两个排序的列表
热门文章
- 第三次学JAVA再学不好就吃翔(part54)--StringBuffer类的添加功能
- 《犯罪心理学》读书笔记(part9)--犯罪心理的主观差异(下)
- SAP 电商云 Spartacus UI CI 脚本分析
- SAP Commerce web service 的扩展
- SAP Spartacus table cell如何通过cxOutlet在运行时动态注入组件
- 如何通过代码获得当前SAP Spartacus Component渲染所基于的slot名称
- SAP Spartacus UserService.get方法的调用时机
- SAP Spartacus批量读取产品的逻辑
- SAP Spartacus里的标准Banner设计
- SAP Fiori Elements原理介绍之类型为Value Help的Smart Field工作原理