Why Feign Fails with LocalDate/LocalDateTime and How to Fix It
When a Spring Cloud Feign client sends a request containing Java 8 date/time types like LocalDate, LocalTime, or LocalDateTime, the response may trigger JSON parse errors because Spring MVC serializes them as arrays, but Feign expects a proper object, which can be resolved by adding Jackson's JSR‑310 module and configuring the ObjectMapper.
Problem
When a Spring Cloud Feign client sends or receives a request that contains Java 8 date‑time types ( LocalDate, LocalTime, LocalDateTime), Jackson may fail to deserialize the value and throw a HttpMessageNotReadableException.
Typical error log
2018-03-13 09:22:58,445 WARN [http-nio-9988-exec-3] org.springframework.web.servlet.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not construct instance of java.time.LocalDate: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator), or perhaps need to add/enable type information?; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.LocalDate: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator), or perhaps need to add/enable type information?) at [Source: java.io.PushbackInputStream@67064c65; line: 1, column: 63] (through reference chain: java.util.ArrayList[0]->com.didispace.UserDto["birthday"])Root cause
Spring MVC’s default Jackson configuration serializes LocalDate as an array of numbers (e.g., [2023,4,10]). Feign, however, expects the JSON field to be a plain ISO‑8601 string (e.g., "2023-04-10"). The mismatch causes Jackson to treat the incoming value as an ArrayList and fail to construct a LocalDate instance.
Solution
Add the Java 8 date‑time module
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>If the project uses Spring Boot’s parent POM, the version is managed automatically.
Register the module and disable timestamp output
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
// Prevent serialization as numeric timestamps
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// Register support for Java 8 date/time types
mapper.registerModule(new JavaTimeModule());
return mapper;
}This configuration makes Jackson write dates in ISO‑8601 format and correctly deserialize them back to LocalDate , LocalTime , and LocalDateTime .
Verify the fix After adding the dependency and the ObjectMapper bean, the endpoint returns a JSON payload such as: {"userName":"alice","birthday":"2023-04-10"} Feign now maps the birthday field to a LocalDate without errors.
Minimal reproducible example
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@RestController
class HelloController {
@PostMapping("/user")
public UserDto echo(@RequestBody UserDto dto) {
return dto;
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
static class UserDto {
private String userName;
private LocalDate birthday;
}With the jackson-datatype-jsr310 dependency and the custom ObjectMapper bean shown above, the birthday field is serialized as an ISO‑8601 string and deserialized correctly by both Spring MVC and Feign clients.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
