Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom deserialization of path variable in spring web starter

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
like image 569
Journeycorner Avatar asked Oct 24 '25 18:10

Journeycorner


1 Answers

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

like image 194
MarkHuntDev Avatar answered Oct 26 '25 07:10

MarkHuntDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!