CDI injection not resolved inside a Jackson Serializer

最后都变了- 提交于 2019-12-13 14:19:27

问题


I'm trying to inject a @RequestScoped object class in a Jackson's JsonSerializer<>:

public class DigitalInputSerializer extends JsonSerializer<DigitalInput>
{
    @Inject private UserRequestResources user;

    @Override
    public void serialize(DigitalInput value, JsonGenerator gen,
            SerializerProvider serializers) throws IOException,
            JsonProcessingException {

        gen.writeStartObject();
        gen.writeStringField("user", this.user.getMe().getUser());

On the last line, this.user is null.

@RequestScoped
public class UserRequestResources
{

This object class is resolved on whereever on other places of the project. I'm using Wildfly 8 J2EE implementation.

EDIT:

I've a main resource where I configure jackson mapper:

@ApplicationScoped
public class SerializationApplicationResources
{

  protected ObjectMapper mapper;

  @PostConstruct
  protected void initialize_resources() throws IllegalStateException
  {
    this.mapper = new ObjectMapper();

    SimpleModule module = new SimpleModule();

    module.addDeserializer(DigitalInput.class, new DigitalInputDeserializer());
    module.addSerializer(DigitalInput.class, new DigitalInputSerializer());

In order to use it, I only annotate my fields with @Inject, and then I've an instance of that.

@Inject protected SerializationApplicationResources jacksonResources;

So then,

this.jacksonResources.getMapper().writeValueAsBytes(entity);

回答1:


There may not be simple way to do that because as others have pointed out, objects that Jackson serializes are not injected automatically as Jackson has no knowledge of how this process would happen.

But Jackson does have extension points for building simple modules that do just that. For example there are Guice and OSGi modules:

  • https://github.com/FasterXML/jackson-module-guice
  • https://github.com/FasterXML/jackson-module-osgi

both of which can support use of Jackson's @JacksonInject annotation (to denote where to inject, and what logical id to use) to find out injectable objects using whatever mechanism module wants to. So it should be possible to implement CDI-injection module, if one does not exist (I am not aware of one, but since it should be doable someone may already have done that).

This assumes that the object you want injected is created by Jackson, based on JSON; in which case CDI implementation itself does not typically have access to it.




回答2:


First possibility:

Inject UserRequestResources into SerializationApplicationResources and pass the injected instance via the constructor to DigitalInputSerializer.

Second possibility:

Make DigitalInputSerializer also managed, e.g. RequestScoped (or even Stateless?), inject it into SerializationApplicationResources and use the injected instance instead of creating objects explicitly - then, the transitively injected UserRequestResources should be initiated as well.




回答3:


Since DigitalInputSerializer is not instantiated by container (this is created by new operator) injections in this class won't work.

If you need quickfix solution you could get UserRequestResources programmatically. Take a look at this post to do it: How to programmatically inject a Java CDI managed bean into a local variable in a static method



来源:https://stackoverflow.com/questions/33101708/cdi-injection-not-resolved-inside-a-jackson-serializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!