[英]Cannot deserialize value of type `java.lang.String` from Array value from mockmvc
public class ModelDTO implements Serializable {
private Long id;
private String datasetName;
@JsonRawValue
private String json;
}
Post API is working fine from postman or swagger if I send the following body.如果我发送以下正文,API 从 postman 或 swagger 可以正常工作。
{
"id": 1,
"datasetName": "Insurance",
"json" : "[{\"ClassName\":\"AAAA\",\"Fields\":[[\"cdsa\",\"csa\"],[\"ca\"]]},{\"ClassName\":\"ca\",\"Fields\":[null]}]"
}
But MockMVC test case is giving follwing error in Spring boot project但是 MockMVC 测试用例在 Spring 引导项目中给出以下错误
Bad Request: JSON parse error: Unexpected character ('C' (code 67)): was expecting comma to separate Object entries;错误请求:JSON 解析错误:意外字符('C'(代码 67)):期望逗号分隔 Object 条目; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('C' (code 67)): was expecting comma to separate Object entries at [Source: (PushbackInputStream);嵌套异常是 com.fasterxml.jackson.core.JsonParseException:意外字符('C'(代码 67)):期望逗号分隔 Object 条目 [来源:(PushbackInputStream); line: 1, column: 89]行:1,列:89]
mockMvc.perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(ModelDTO))).andExpect(status().isCreated());
I believe you don't actually need @JsonRawValue
so try removing it:我相信你实际上并不需要@JsonRawValue
所以尝试删除它:
public class ModelDTO implements Serializable {
private Long id;
private String datasetName;
private String json;
}
my json were not properly set.我的 json 设置不正确。 it requires backword slash for parsing.它需要反斜杠进行解析。 I have added extra two backword slash like '\' and now it is working.我添加了额外的两个反义词斜杠,如“\”,现在它可以正常工作了。
private static final String DEFAULT_JSON = "\"[{\\\"ClassName\\\":\\\"Health Aditya Birla\\\",\\\"Fields\\\":[[\\\"Insured Person's Details\\\",\\\"Gender\\\",\\\"Member ID\\\"],[\\\"Policy Details\\\",\\\"Insured Person's Details\\\"],[\\\"Premium Certificate\\\"]]},{\\\"ClassName\\\":\\\"Health Care\\\",\\\"Fields\\\":[[\\\"Details of Insured\\\",\\\"Relationship\\\",\\\"Date Of Birth\\\"],[\\\"Mobile No\\\"],[\\\"Gross Premium\\\",\\\"Goods & Services Tax\\\"]]}]\"";
I have referred following link to solve this issue.我已参考以下链接来解决此问题。
In my case I had incorrect Type definition.就我而言,我的类型定义不正确。 eg例如
public class ModelDTO implements Serializable {
private Long id;
private String datasetName;
private String part;
}
So I had to correct it as:所以我不得不将其更正为:
public class ModelDTO implements Serializable {
private Long id;
private String datasetName;
private List<String> part;
}
That solved my issue.那解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.