简体   繁体   English

无法从 mockmvc 的数组值中反序列化 `java.lang.String` 类型的值

[英]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.我已参考以下链接来解决此问题。

enter link description here 在此处输入链接描述

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.

相关问题 无法反序列化 `java.util.LinkedHashMap 类型的值<java.lang.string,java.lang.string> ` 来自 Array 或什么是我的字段的正确类型</java.lang.string,java.lang.string> - Cannot deserialize value of type `java.util.LinkedHashMap<java.lang.String,java.lang.String>` from Array or what would be a correct type of my field 面对 JSON 解析问题,无法从数组值 jackson.databind.exc.MismatchedInputException 反序列化类型为“java.lang.String”的值 - Facing JSON parse issue, Cannot deserialize value of type `java.lang.String` from Array value jackson.databind.exc.MismatchedInputException java.lang.String 类型的值数组无法转换为 JSONObject - Value Array of type java.lang.String cannot be converted to JSONObject 如何修复“ JCO_ERROR_CONVERSION:无法将值从类型java.lang.String转换为STRUCTURE” - How to fix 'JCO_ERROR_CONVERSION: Cannot convert a value from type java.lang.String to STRUCTURE' 从数据库获取对象时,无法将类型为java.lang.String的值连接转换为JSONObject - Value Connection of type java.lang.String cannot be converted to JSONObject in getting objects from the database 无法将“java.lang.String”类型的值转换为所需类型 - Cannot convert value of type 'java.lang.String' to required type java.lang.string类型的Java值无法转换为jsonobject - Java value of type java.lang.string cannot be converted to jsonobject Spring JPAF无法将值“为什么?”从类型[java.lang.String]转换为类型[java.lang.Long]。 - Spring JPAFailed to convert from type [java.lang.String] to type [java.lang.Long] for value 'Why?' 类型为java.lang.String的值yes无法转换为JSONObject - Value yes of type java.lang.String cannot be converted to JSONObject java.lang.String类型的值数据库无法转换为JSONObject - Value Database of type java.lang.String cannot be converted to JSONObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM