不调用带有spring boot webflux的java自定义转换器

yhived7q  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(282)

使用 spring-boot-starter-webflux 在Java14的2.3.3版本中,我尝试注册一个自定义转换器,将字符串输入转换为枚举。我遇到的问题是,在执行时,底层jackson库会尝试转换字符串,而不考虑我注册的自定义转换器。
这是我使用的控制器:

@RestController
public class MyController {

    private final MyService myService;

    @Autowired
    public MyController(MyService myService) {
        this.myService = myService;
    }

    @PostMapping(value = "/subscriptions")
    public CreateSubscriptionOutput createSubscription(@RequestBody @Valid CreateSubscriptionInput input) throws Exception {
        return myService.createSubscription(input);
    }

}

输入定义如下:

public class CreateSubscriptionInput {

    private EventType eventType;

    public CreateSubscriptionInput() {
        this.eventType = EventType.A;
    }

    public CreateSubscriptionInput(EventType type) {
        this.eventType = type;
    }

    public EventType getEventType() {
        return this.eventType;
    }

}

public enum EventType implements SafeEnum {

    A(0L, "a"),
    B(1L, "b"),

    private final long id;
    private final String name;

    public static EventType from(long id) {
        return (EventType) Enums.from(EventType.class, id);
    }

    public static EventType from(String name) {
        return (EventType) Enums.from(EventType.class, name);
    }

    private EventType(long id, String name) {
        this.id = id;
        this.name = name;
    }

    public long getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

}

public interface SafeEnum {

    long getId();

    String getName();

}

public final class Enums {

    public static <E extends Enum<E> & SafeEnum> E from(Class<E> clazz, long id) {
        for (E e : EnumSet.allOf(clazz)) {
            if (e.getId() == id) {
                return e;
            }
        }

        throw new IllegalArgumentException("Unknown " + clazz.getSimpleName() + " id: " + id);
    }

    public static <E extends Enum<E> & SafeEnum> E from(Class<E> clazz, String name) {
        if (name == null) {
            return null;
        }

        for (E e : EnumSet.allOf(clazz)) {
            if (e.getName().equals(name)) {
                return e;
            }
        }

        throw new IllegalArgumentException("Unknown " + clazz.getSimpleName() + " name: " + name);
    }

}

自定义转换器的定义和注册如下:

@Configuration
@EnableWebFlux
public class ApplicationConfiguration implements WebFluxConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new Converter<String, EventType>() {
            @Override
            public EventType convert(String source) {
                return EventType.from(source);
            }
        });
    }

目的是将“a”转换为 EventType.A .
当我按如下方式调用rest资源时:

curl --location --request POST 'http://localhost:8081/subscriptions' \
     --header 'Content-Type: application/json' \
     --data-raw '{
         "eventType": "a",
     }'

我得到一个400错误,内部错误如下:
json解码错误:无法反序列化类型的值 my.app.EventType 从字符串“a”:枚举类[a,b]不接受的值之一
这让我觉得转换器从来没有被调用过。在debug中运行代码证实了这个假设。在debug中设置根记录器似乎不会打印有关已注册转换器的信息。
我注册转换器的方式有问题吗?少了什么?

db2dz4w8

db2dz4w81#

不清楚为什么在反序列化用注解的dto中的值时不使用注册的转换器 @RequestBody . 官方文件似乎没有描述这个案件。希望,一个解决方法是使用 @JsonCreator 注解:

public enum EventType implements SafeEnum {

    A(0L, "a"),
    B(1L, "b"),

    private final long id;
    private final String name;

    public static EventType from(long id) {
        return (EventType) Enums.from(EventType.class, id);
    }

    @JsonCreator
    public static EventType from(String name) {
        return (EventType) Enums.from(EventType.class, name);
    }

    private EventType(long id, String name) {
        this.id = id;
        this.name = name;
    }

    public long getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

}

这种方法不太灵活,需要对所有枚举进行重复,但至少它具有按预期工作的优点。

相关问题