T
T
TonyDevel2019-02-28 10:44:23
Java
TonyDevel, 2019-02-28 10:44:23

Why doesn't Spring use Jackson for the parameter in the endpoint?

Good afternoon.
There is a controller, the endpoint in this controller has a requested parameter, which is an Enum, below is an example:

@RequestMapping("/")
public void foo(@RequestParam(value="enum") Enum enum) {
//some logic
}

The enam code itself:
public enum Enum {
        ONE, TWO;

    @JsonCreator
    public static Enum safeValueOf(String value) {
        return Stream.of(Enum.values())
                .filter(i -> i.name().equals(value))
                .findAny()
                .orElse(null);
    }
}

The problem is the following: when I pass as a parameter a value that is not in the enam, spring throws an Exception MethodArgumentTypeMismatchException because it uses a StringToEnumConverter that explicitly calls Enum.valueOf(value).
How to force Spring to use Jackson and not its own converter?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kosarev, 2019-03-11
@jaxtr

Why use Jackson for this?
For that matter, it would be much more logical to write an implementation of org.springframework.core.convert.converter.Converter that will cast the string to the desired value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question