Vue formdata传List参数到后台接口报错Failed to convert property value of type ‘java.lang.String’ to required type ‘java.util.List’

  • A+
所属分类:Vue

今天前后端联调时报错

org.springframework.validation.BeanPropertyBindingResult: 4 errors↵Field error in object 'expert' on field 'expertEduList': rejected value [[object Object]]; codes [typeMismatch.expert.expertEduList,typeMismatch.expertEduList,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [expert.expertEduList,expertEduList]; arguments []; default message [expertEduList]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'expertEduList'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.cjsz.tjzbk.entities.Edu' for property 'expertEduList[0]': no matching editors or conversion strategy found]↵Field error in object 'expert' on field 'expertReportList': rejected value [[object Object]]; codes [typeMismatch.expert.expertReportList,typeMismatch.expertReportList,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [expert.expertReportList,expertReportList]; arguments []; default message [expertReportList]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'expertReportList'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.cjsz.tjzbk.entities.Report' for property 'expertReportList[0]': no matching editors or conversion strategy found]↵Field error in object 'expert' on field 'expertResearchAreaList': rejected value [[object Object]]; codes [typeMismatch.expert.expertResearchAreaList,typeMismatch.expertResearchAreaList,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [expert.expertResearchAreaList,expertResearchAreaList]; arguments []; default message [expertResearchAreaList]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'expertResearchAreaList'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.cjsz.tjzbk.entities.ResearchArea' for property 'expertResearchAreaList[0]': no matching editors or conversion strategy found]↵Field error in object 'expert' on field 'expertResumeList': rejected value [[object Object]]; codes [typeMismatch.expert.expertResumeList,typeMismatch.expertResumeList,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [expert.expertResumeList,expertResumeList]; arguments []; default message [expertResumeList]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'expertResumeList'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.cjsz.tjzbk.entities.Resume' for property 'expertResumeList[0]': no matching editors or conversion strategy found]"
status: "ERROR"
__proto__: Object

后台接收的Bean里有几个字段是List<>型的。比如List<Resume>之类的。百度后说传List到后台要用JSON.stringify()先转成Json,改了之后报错依旧,仔细看了报错的信息,原来是不能从String转成Resume类型。。我晕,意思就是不能用List<Resume>接收了,又一通百度,试了半天,发现后台用String型接收,再转成java对象这种方式是可行的。

那么问题来了,我如何将接收的String型转成我想要的
List<Resume> ???网上说的是可以转成一般的java对象用JsonObject即可,但是能否转成List<Resume> 这种都没有明确说明。由于我用的是阿里的fastJson,我想这个说不定支持,找了下里面的方法,果然有一个JSON.parseArray方法,返回的是List<T>,就是它了!!测试可行。代码如下:

List expertResumeList = JSON.parseArray(expert.getExpertResumeListStr(), Resume.class);

前端传参数时还是加上JSON.stringify() 即可

ZPY

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: