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);