1

I have written custom JsonCreator for enum

@GetMapping(value = "/path/{pathVar1}")
  public Response getValue(
      @PathVariable String pathVar1,
       @RequestParam(required = false, name = "type") MyEnum type)

Enum

@AllArgsConstructor
@Getter
public enum MyEnum {

  A_12(Set.of("a", "12"));

  private Set<String> aliases;

  @JsonCreator
  public static MyEnum forValues(@JsonProperty("type") String type) {
    return Arrays.stream(MyEnum.values())
        .filter(s -> s.getAliases().stream().anyMatch(a -> StringUtils.equalsIgnoreCase(a, type)))
        .findFirst()
        .orElseThrow(() -> new RuntimeException("aa"));
  }

Its not considering jsoncreator for enum.

Caused by: java.lang.IllegalArgumentException: No enum constant MyEnum.12

Am I missing any thing.

Patan
  • 17,073
  • 36
  • 124
  • 198

1 Answers1

1

@RequestParam does not use a jackson deserializer. So @JsonCreator is not used.

By default, Spring uses Converter to deserialize @RequestParam and @PathVariable values.

So if you want to convert path values, create a custom converter.

Check out this link. I hope it can help you.

bidulgi69
  • 51
  • 2