How to make Jackson read this invalid JSON?

3.4k views Asked by At

In a Spring Boot app, using Spring Cloud OpenFeign, I'm about to read an endpoint for a Talend server but just noticed this isn't valid JSON (missing double quotes around the word transactions):

{
  transactions: [
    "some-uuid",
    "another-one-uuid",
    "and-so-on-uuid"
  ]
}

My model class:

@Data
@SuperBuilder
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.ALWAYS)
public class Transactions {

    private List<String> transactions;

}

My service/client class:

@Service
@FeignClient(value = "nativeTalend", url = "http://talend-server:8180/talendmdm/services/rest/")
public interface TalendNativeAPI {

    @GetMapping(path = "transactions", produces = MediaType.APPLICATION_JSON_VALUE)
    Transactions transactions();

}

Note: it needs the "produces" in the @GetMapping annotation because otherwise the server answers with a 500 HTTP error...

Here is the OpenFeign log:

[TalendNativeAPI#transactions] ---> GET http://talend-server:8180/talendmdm/services/rest/transactions HTTP/1.1
[TalendNativeAPI#transactions] Accept: application/json
[TalendNativeAPI#transactions] Authorization: Basic ==xxx==xxx==xxx==
[TalendNativeAPI#transactions] ---> END HTTP (0-byte body)
[TalendNativeAPI#transactions] <--- HTTP/1.1 200 (282ms)
[TalendNativeAPI#transactions] cache-control: no-cache
[TalendNativeAPI#transactions] content-type: application/json;charset=UTF-8
[TalendNativeAPI#transactions] date: Sun, 27 Sep 2020 13:54:50 GMT
[TalendNativeAPI#transactions] expires: Thu, 01 Jan 1970 00:00:00 GMT
[TalendNativeAPI#transactions] pragma: No-cache
[TalendNativeAPI#transactions] set-cookie: JSESSIONID=XXX_XXX_XXX; Path=/talendmdm; HttpOnly
[TalendNativeAPI#transactions] transfer-encoding: chunked
[TalendNativeAPI#transactions] x-content-type-options: nosniff
[TalendNativeAPI#transactions] x-frame-options: DENY
[TalendNativeAPI#transactions] x-xss-protection: 1; mode=block
[TalendNativeAPI#transactions] <--- END HTTP (17-byte body)

and the exception stacktrace (I run this through a unit test):

feign.codec.DecodeException: Error while extracting response for type [class my.company.app.Transactions] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('t' (code 116)): was expecting double-quote to start field name; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('t' (code 116)): was expecting double-quote to start field name
 at [Source: (ByteArrayInputStream); line: 1, column: 3]
    at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:180)
    at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:140)
    at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78)
    at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)
    at com.sun.proxy.$Proxy147.transactions(Unknown Source)
    at my.company.app.TalendIntegrationTest.test_noTransactions(TalendIntegrationTest.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Caused by: org.springframework.web.client.RestClientException: Error while extracting response for type [class my.company.app.Transactions] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('t' (code 116)): was expecting double-quote to start field name; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('t' (code 116)): was expecting double-quote to start field name
 at [Source: (ByteArrayInputStream); line: 1, column: 3]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:117)
    at org.springframework.cloud.openfeign.support.SpringDecoder.decode(SpringDecoder.java:59)
    at org.springframework.cloud.openfeign.support.ResponseEntityDecoder.decode(ResponseEntityDecoder.java:62)
    at feign.optionals.OptionalDecoder.decode(OptionalDecoder.java:36)
    at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:176)
    ... 36 more
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('t' (code 116)): was expecting double-quote to start field name; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('t' (code 116)): was expecting double-quote to start field name
 at [Source: (ByteArrayInputStream); line: 1, column: 3]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:245)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:227)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:102)
    ... 40 more
Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('t' (code 116)): was expecting double-quote to start field name
 at [Source: (ByteArrayInputStream); line: 1, column: 3]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1804)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:693)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:591)
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleOddName(UTF8StreamJsonParser.java:1996)
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._parseName(UTF8StreamJsonParser.java:1647)
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:733)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:155)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4014)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3085)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:239)
    ... 42 more

How can I configure/"fix" Jackson to make it still read this falsy JSON from this specific endpoint (with the whole context of Spring Boot + Spring Cloud OpenFeign + Jackson auto-declared bean (by Spring)?

Thanks.

1

There are 1 answers

1
aeberhart On BEST ANSWER

The ALLOW_UNQUOTED_FIELD_NAMES feature should do the trick:

public class Unquote {

  public static void main(String[] args) throws Exception {
    ObjectMapper om = new ObjectMapper()
        .configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    JsonNode tree = om.readTree("{transactions:[\"some.uuid\"]}".getBytes());
    System.out.println(tree);
  }
}

This answer explains how to do this in the Spring context: Configuring ObjectMapper in Spring