1

How can I inject EJBs into the XmlAdapters?

The idea is I want to get list of IDs by rest API and convert this array of IDs to List of Objects for Entity Object. For example:

public class Post {
    List<Category> categories;
    ...
}

public class AdaptedPost {
    List<Long> categories;
    ...
}

public class PostAdapter extends XmlAdapter<AdaptedPost, Post> {

    @EJB
    CategoryFacade categoryFacade;

    @Override
    public Post unmarshal(final AdaptedPost adaptedPost) throws Exception {
        // Use facade class to retrieve category object from ID
    }
    ...    
}
user1079877
  • 9,008
  • 4
  • 43
  • 54

1 Answers1

2

There is no support defined for injection into javax.xml.bind.annotation.adapters.XmlAdapter objects.

You will need to acquire your EJB the old fashioned way using a JNDI lookup.

Hash
  • 4,647
  • 5
  • 21
  • 39
Steve C
  • 18,876
  • 5
  • 34
  • 37
  • Is this fast enough? Is there any other solution in my case? I mean is it good idea at all? – user1079877 Feb 27 '15 at 05:13
  • 1
    Do the lookup once and cache it somewhere. Alternatively, if the code has been invoked from some other EJB, inject into that one and stash it in a ThreadLocal (with the usual caveats regarding correct cleanup) – Steve C Feb 27 '15 at 05:16