0

I am trying to do the Rest Get call using oAuth2RestTemplate.getForEntity to service and expecting json reply to be mapped in one of my model class but as the mapping class uses local date (Java 8 concept) I am getting the below error at the time of desterilization :

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not construct instance of java.time.LocalDate: no String-argument constructor/factory method to deserialize from String value ('1988-02-08')

at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@96ce9eb2; line: 1,

Here is the snippet of my code :

ResponseEntity<Some mapping class> response = this.oAuth2RestTemplate.getForEntity(this.ServiceUrl, Some mapping class.class, inputParameter);

I am aware that this is happening because my model class maps birth date in local date which is introduced in java 8 and my oAuth2RestTemplate is not supporting it. using below code solves the problem

ObjectMapper mapperObj = new ObjectMapper();
mapperObj.registerModule(new JavaTimeModule());
mapperObj.disable(WRITE_DATES_AS_TIMESTAMPS);

but in this case I need to take my service response in string and then I will need to convert it. I need a solution with oAuth2RestTemplate.Can someone help.

  • Which Spring version are you using and which Jackson version? A recent version of Spring will detect the `JavaTimeModule` and configure the ObjectMapper accordingly. Else you can always configure the `JacksonMessageConverter` to use the module. – M. Deinum Feb 11 '21 at 12:26
  • I am using spring version 4.1.6 and using 2.6.0 jackson version – Avinash Chikane Feb 11 '21 at 12:43
  • Then configure the `RestTemplate`s `JacksonHttpMessageConverter` to use the correct `ObjectMapper`. – M. Deinum Feb 11 '21 at 13:22
  • Tried doing this before the service call : MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter(); messageConverter.setPrettyPrint(false); ObjectMapper mapperObj = new ObjectMapper(); mapperObj.registerModule(new JavaTimeModule()); mapperObj.disable(WRITE_DATES_AS_TIMESTAMPS); messageConverter.setObjectMapper(mapperObj); oAuth2RestTemplate.getMessageConverters().add(0, messageConverter); but getting the same error. – Avinash Chikane Feb 11 '21 at 13:50
  • That won't work as you need to configure that at configuration time.Also be aware of the fact that you are using an old Spring version (the current is 5.4). – M. Deinum Feb 11 '21 at 13:57

0 Answers0