Answer the question
In order to leave comments, you need to log in
How to disable field deserialization in jackson?
@Data
public class TestObject {
private String name;
@JsonRawValue
private String result;
}
@RestController
@RequestMapping("test")
public class TestController {
@GetMapping
public String get() {
return "{" +
"\"name\": \"vasya\"," +
"\"result\": {\"bla\": \"bla\"}" +
"}";
}
}
public static void main(String[] args) {
RestTemplate template = new RestTemplate();
ResponseEntity<TestObject> exchange = template.exchange("http://localhost:8080/test", HttpMethod.GET, null, TestObject.class);
System.out.println(exchange.getBody());
}
}
Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
Answer the question
In order to leave comments, you need to log in
it happened
@Data
public class TestObject {
private String name;
@JsonDeserialize(using = ResultDeserializer.class)
private String result;
public static class ResultDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
JsonNode node = mapper.readTree(jp);
return mapper.writeValueAsString(node);
}
}
}
It's not entirely clear what you need...
@JsonIgnore
maybe it will help?...
you need json as a string in the result field. I cannot change the controller response (for example, wrap it in quotes).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question