在 Springboot 中使用 Jackson2 进行反序列化 application/json 响应只是 "A String"
Deserializing using Jackson2 in Springboot an application/json response that is just "A String"
我在我的 Springboot 应用程序中调用外部 API 并使用 MappingJackson2HttpMessageConverter
转换 application/json
响应,但我 运行 变成问题作为响应只是 "Some message indicating success/failure"
而不是像 { 'property': 'value' }
.
这样更典型的 JSON 对象
我具体得到的错误是:
JSON parse error: Cannot construct instance of `<custom class>` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('Bundle is not available'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `<custom class>` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('Bundle is not available')\n at [Source: (ByteArrayInputStream); line: 1, column: 1]
仅供参考,我配置JSON解串器的代码是这样的:
public List<HttpMessageConverters<?>> getConverters() {
List<HttpMessageConverter<?>> converters = new ArrayList<>();
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
converters.add(converter);
return converters;
}
我想知道如何配置 MappingJackson2HttpMessageConverter
以支持裸字符串?
所以我通过编写自己的消息转换器解决了这个问题。类似于:
class StringConverter extends AbstractJsonHttpMessageConverter {
@Override
protected Object readInternal(Type type, Reader reader) throws Exception {
int currentChar;
StringBuilder str = new StringBuilder();
while((currentChar = reader.read()) != -1) {
str.append((char) currentChar);
}
ObjectMapper om = new ObjectMapper();
return objectMapper.readValue(str.toString(), Object.class);
}
@Override
protected void writeInternal(Object o, Type type, Writer writer) throws Exception {
// not required for my use case
}
}
有了这个,字符串响应被存储在字符串 类 中,JSON 响应被放入链接的哈希映射中。如果有人有更好的方法来解决这个问题,请 post 因为我确信这远不是最好的方法。
我在我的 Springboot 应用程序中调用外部 API 并使用 MappingJackson2HttpMessageConverter
转换 application/json
响应,但我 运行 变成问题作为响应只是 "Some message indicating success/failure"
而不是像 { 'property': 'value' }
.
我具体得到的错误是:
JSON parse error: Cannot construct instance of `<custom class>` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('Bundle is not available'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `<custom class>` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('Bundle is not available')\n at [Source: (ByteArrayInputStream); line: 1, column: 1]
仅供参考,我配置JSON解串器的代码是这样的:
public List<HttpMessageConverters<?>> getConverters() {
List<HttpMessageConverter<?>> converters = new ArrayList<>();
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
converters.add(converter);
return converters;
}
我想知道如何配置 MappingJackson2HttpMessageConverter
以支持裸字符串?
所以我通过编写自己的消息转换器解决了这个问题。类似于:
class StringConverter extends AbstractJsonHttpMessageConverter {
@Override
protected Object readInternal(Type type, Reader reader) throws Exception {
int currentChar;
StringBuilder str = new StringBuilder();
while((currentChar = reader.read()) != -1) {
str.append((char) currentChar);
}
ObjectMapper om = new ObjectMapper();
return objectMapper.readValue(str.toString(), Object.class);
}
@Override
protected void writeInternal(Object o, Type type, Writer writer) throws Exception {
// not required for my use case
}
}
有了这个,字符串响应被存储在字符串 类 中,JSON 响应被放入链接的哈希映射中。如果有人有更好的方法来解决这个问题,请 post 因为我确信这远不是最好的方法。