I have the following simple test
import org.springframework.web.client.RestTemplate;
import pages.TimezonePage;
class GETSimple
{
private RestTemplate restTemplate;
private String URL = "http://worldtimeapi.org/api/timezone/America/Los_Angeles";
@BeforeEach
void setUp()
{
restTemplate = new RestTemplate();
}
@Test
void GETasPOJO()
{
TimezonePage response = restTemplate.getForObject(URL, TimezonePage.class);
assertEquals("PDT", response.getAbbreviation());
assertTrue(response.getDst());
}
}
which works fine with TimezonePage class that has getters and setters.
The relevant portion of the pom.xml is below
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.3</version>
</dependency>
I created a Java 14 record TimezoneRecord with the same variables as TimezonePage but without getters and setters. Then I replaced the get call with
TimezoneRecord response = restTemplate.getForObject(URL, TimezoneRecord.class);
There was no syntax error. When running the test, I received the following exception
org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class pages.TimezoneRecord]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `pages.TimezoneRecord` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
Is there any way to resolve it? Or I cannot use jackson with records as POJOs? Thanks, Vladimir