【问题标题】:Spring Data Rest: @Autowire in Custom JsonDeserializerSpring Data Rest:自定义 JsonDeserializer 中的 @Autowire
【发布时间】:2019-12-18 20:42:48
【问题描述】:

我正在尝试将组件自动装配到自定义 JsonDeserializer 中,但即使我找到了以下建议,也无法正确完成:

我的最终目标是接受不同微服务中资源的 URL,并仅在本地存储资源的 ID。但我不想只从 URL 中提取 ID,还要验证 URL 的其余部分是否正确。

我已经尝试了很多东西,但我对我尝试过的东西有点迷失了,但我相信我尝试了上面链接中提到的所有东西。我为 SpringHandlerInstantiator、Jackson2ObjectMapperBuilder、MappingJackson2HttpMessageConverter、RestTemplate 等创建了大量的 bean,还尝试在 RepositoryRestConfigurer#configureJacksonObjectMapper 中设置 SpringHandlerInstantiator。

我正在使用 Spring Boot 2.1.6.RELEASE,这让我觉得有些东西可能已经改变了,因为一些链接的线程已经很老了。

这是我的最后一次尝试:

@Configuration
public class JacksonConfig {

    @Bean
    public HandlerInstantiator handlerInstantiator(ApplicationContext applicationContext) {
        return new SpringHandlerInstantiator(applicationContext.getAutowireCapableBeanFactory());
    }

}
@Configuration
public class RestConfiguration implements RepositoryRestConfigurer {

    @Autowired
    private Validator validator;

    @Autowired
    private HandlerInstantiator handlerInstantiator;

    @Override
    public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
        validatingListener.addValidator("beforeCreate", validator);
        validatingListener.addValidator("beforeSave", validator);
    }

    @Override
    public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
        objectMapper.setHandlerInstantiator(handlerInstantiator);
    }

}
@Component
public class RestResourceURLSerializer extends JsonDeserializer<Long> {

    @Autowired
    private MyConfig config;

    @Override
    public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        ServiceConfig serviceConfig = config.getServices().get("identity");
        URI serviceUri = serviceConfig.getExternalUrl();
        String servicePath = serviceUri.getPath();

        URL givenUrl = p.readValueAs(URL.class);
        String givenPath = givenUrl.getPath();

        if (servicePath.equals(givenPath)) {
            return Long.parseLong(givenPath.substring(givenPath.lastIndexOf('/') + 1));
        }

        return null;
    }

}

我不断收到 NullPointerException POST 到 API 端点的信息,该端点使用上面的 JsonDeserializer 反序列化。

【问题讨论】:

    标签: spring-boot jackson spring-data-rest


    【解决方案1】:

    我能够通过将我的反序列化器构造函数标记为接受一个参数(并因此删除空构造函数)并将构造函数标记为@Autowired 来解决类似的问题。

     public class MyDeserializer extends JsonDeserializer<MyEntity> {    
        private final MyBean bean;
    
        // no default constructor
        @Autowired
        public MyDeserializer(MyBean bean){
            this.bean = bean
        }
        ...
    } 
    
    @JsonDeserialize(using = MyDeserializer.class)
    public class MyEntity{...}
    

    我的实体标有 @JsonDeserialize 注释,因此我不必使用 ObjectMapper 显式注册它。

    【讨论】:

      猜你喜欢
      • 2015-06-22
      • 2018-04-18
      • 2021-05-13
      • 2015-03-25
      • 1970-01-01
      • 2014-11-16
      • 2018-03-17
      • 2017-03-06
      • 2013-11-23
      相关资源
      最近更新 更多