What is the correct way to register a custom deserializer for a path variable in Spring web reactive?
Example:
@GetMapping("test/{customType}")
public String test(@PathVariable CustomType customType) { ...
I tried ObjectMapperBuilder, ObjectMapper and directly via @JsonDeserialize(using = CustomTypeMapper.class) but it wont register:
Response status 500 with reason "Conversion not supported."; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type '...CustomType'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type '...CustomType': no matching editors or conversion strategy found
For path variables deserialization you don't need to involve jackson, you can use org.springframework.core.convert.converter.Converter
For example:
@Component
public class StringToLocalDateTimeConverter
implements Converter<String, LocalDateTime> {
@Override
public LocalDateTime convert(String source) {
return LocalDateTime.parse(
source, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
}
@GetMapping("/findbydate/{date}")
public GenericEntity findByDate(@PathVariable("date") LocalDateTime date) {
return ...;
}
Here's an article with examples
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With