如何使用spring转换服务转换泛型类型

qlvxas9a  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(250)

我在转换泛型时遇到了一个问题,似乎没有查找泛型类型并调用首先注册为notificationrequestdto的转换器。
例子:

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        DefaultConversionService conversionService = new DefaultConversionService();

        conversionService.addConverter(new PushNotificationRequestToResponseDtoConverter());
        conversionService.addConverter(new EmailNotificationRequestToResponseDtoConverter());

        conversionService.convert(
                new NotificationRequestDto<>(new PushChannelDto(), "This is push request content"),
                NotificationResponseDto.class
        );

        conversionService.convert(
                new NotificationRequestDto<>(new EmailChannelDto(), "This is email request content"),
                NotificationResponseDto.class
        );

        SpringApplication.run(DemoApplication.class, args);
    }

    @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@class")
    @JsonIgnoreProperties(ignoreUnknown = true)
    public interface ChannelDto {
        String getType();
    }

    public static class PushChannelDto implements ChannelDto {
        private final String type;

        public PushChannelDto() {
            this.type = "PUSH";
        }

        @Override
        public String getType() {
            return type;
        }
    }

    public static class EmailChannelDto implements ChannelDto {
        @JsonProperty("type")
        private final String type;

        public EmailChannelDto() {
            this.type = "EMAIL";
        }

        @Override
        public String getType() {
            return type;
        }
    }

    public static class NotificationRequestDto<T extends ChannelDto> {
        private final T channel;
        private final String content;

        public NotificationRequestDto(T channel, String content) {
            this.channel = channel;
            this.content = content;
        }

        public T getChannel() {
            return channel;
        }

        public String getContent() {
            return content;
        }
    }

    public static class NotificationResponseDto<T extends ChannelDto> {
        private final T channel;
        private final String content;

        public NotificationResponseDto(T channel, String content) {
            this.channel = channel;
            this.content = content;
        }

        public T getChannel() {
            return channel;
        }

        public String getContent() {
            return content;
        }
    }

    public static class PushNotificationResponseDto extends NotificationResponseDto<PushChannelDto> {
        private final String destination;

        public PushNotificationResponseDto(String content, String destination) {
            super(new PushChannelDto(), content);
            this.destination = destination;
        }

        public String getDestination() {
            return destination;
        }
    }

    public static class EmailNotificationResponseDto extends NotificationResponseDto<EmailChannelDto> {
        private final String subject;

        public EmailNotificationResponseDto(String content, String subject) {
            super(new EmailChannelDto(), content);
            this.subject = subject;
        }

        public String getSubject() {
            return subject;
        }
    }

    public static class PushNotificationRequestToResponseDtoConverter implements Converter<NotificationRequestDto<PushChannelDto>, NotificationResponseDto<PushChannelDto>> {
        @Override
        public NotificationResponseDto<PushChannelDto> convert(NotificationRequestDto<PushChannelDto> request) {
            return new PushNotificationResponseDto("Here goes push content", "/destination");
        }
    }

    public static class EmailNotificationRequestToResponseDtoConverter implements Converter<NotificationRequestDto<EmailChannelDto>, NotificationResponseDto<EmailChannelDto>> {
        @Override
        public NotificationResponseDto<EmailChannelDto> convert(NotificationRequestDto<EmailChannelDto> request) {
            return new EmailNotificationResponseDto("Here goes email content", "Email subject");
        }
    }
}

在转换器中定义具体类型可以解决问题,但这不是我的情况。

class PushNotificationRequestDto extends NotificationRequestDto<PushChannelDto> {...}
class EmailNotificationRequestDto extends NotificationRequestDto<EmailChannelDto> {...}

Converter<PushNotificationRequestDto, NotificationResponseDto<PushChannelDto>>
Converter<EmailNotificationRequestDto, NotificationResponseDto<EmailChannelDto>>

有这种转换器能使它工作吗?

Converter<NotificationRequestDto<PushChannelDto>, NotificationResponseDto<PushChannelDto>>
Converter<NotificationRequestDto<EmailChannelDto>, NotificationResponseDto<EmailChannelDto>>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题