[英]Deserializing json as List<Object>
I have json:我有 json:
"taxLevels": [{
"code": "VAT",
"percentage": 19.0
}
]
This is truly List<TTaxLevel>
这确实是List<TTaxLevel>
I have Model.class :我有Model.class :
public class Model{
private final List<TTaxLevel> taxLevels;
}
And TTaxLevel.class :和TtaxLevel.class :
@NoArgsConstructor
public class TTaxLevel {
private String code;
private Double percentage;
}
But I receive error here:但我在这里收到错误:
[Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of java.util.LinkedHashMap<java.lang.String,java.lang.String> out of START_ARRAY token; [Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of java.util.LinkedHashMap<java.lang.String,java.lang.String> out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.LinkedHashMap<java.lang.String,java.lang.String> out of START_ARRAY token at [Source: (PushbackInputStream); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.LinkedHashMap<java.lang.String,java.lang.String> out of START_ARRAY token at [Source: (PushbackInputStream); line: 36, column: 19] (through reference chain: ...Model["taxLevels"])]]行:36,列:19](通过参考链:...Model["taxLevels"])]]
Can I force somehow jackson
to expect type here ArrayList
instead of Map
?我可以以某种方式强制jackson
在这里ArrayList
而不是Map
吗? This is an issue.这是一个问题。
This is deserialization code:这是反序列化代码:
Model model = new ObjectMapper().readValue(content, Model .class);
Please add getter/setter methods to you classes and provide no-arg constructor.请为您的类添加 getter/setter 方法并提供无参数构造函数。 Also you json is not completely valid, it should be:另外你 json 不完全有效,应该是:
{
"taxLevels": [
{
"code": "VAT",
"percentage": 19.0
}
]
}
I trided putting it inside resource folder, fetch and then response to a sample request.我尝试将它放在资源文件夹中,获取然后响应示例请求。 Here is the sample code:这是示例代码:
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
class Model {
private List<TTaxLevel> taxLevels;
}
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
class TTaxLevel {
private String code;
private Double percentage;
}
@RequestMapping("tax-level")
@RestController
class SampleRequestBody {
private final ObjectMapper objectMapper;
SampleRequestBody(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@PostMapping
public Map<String, Model> taxLevel(@RequestBody Model model) throws JsonProcessingException {
final Map<String, Model> response = new HashMap<>(1);
response.put("data", model);
return response;
}
@GetMapping
public Model getTaxLevel() throws IOException {
InputStream inputStream = new ClassPathResource("tax-level.json").getInputStream();
return objectMapper.readValue(inputStream, Model.class);
}
}
So there are two issues:所以有两个问题:
{
"taxLevels": [{
"code": "VAT",
"percentage": 19.0
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.