I am building an application providing a JAX-RS REST service using JPA (EclipseLink). When exposing User entities over JSON, I am using the @XmlTransient
annotation on some fields (e.g. the password field) to hide them from the JSON representation. When sending a create or update (POST/PUT) operation, I would like to populate the missing fields again so JPA will correctly perform the operation.
My current approach is that I have a custom JsonDeserializer
that is used to deserialize the User and to add the missing fields. For this I would like to inject (using @Inject
) a UserFacadeREST
bean which handles the JPA-stuff. However, this injection fails and the bean instance is null
(which then of course causes a NullPointerException
).
My UserFacadeREST
bean is annoted as follows:
@Stateless
@LocalBean
@Path(UserFacadeREST.PATH)
public class UserFacadeREST extends AbstractFacade<User> {
//...
}
My UserDeserilizer
(custom JsonDeserializer
):
public class UserDeserializer extends JsonDeserializer<User> {
@Inject
private UserFacadeREST userFacade;
@Override
public User deserialize(JsonParser parser, DeserializationContext context) throws IOException,
JsonProcessingException {
JsonNode node = parser.getCodec().readTree(parser);
int userId = (Integer) ((IntNode) node.get("userID")).numberValue();
System.out.println(userId);
User user = userFacade.find(userId); // This line produces the NullPointerException
return user;
}
}
which I then use on my User entity with @JsonDeserialize
:
@Entity
@Table(name = "User")
@XmlRootElement
@JsonDeserialize(using = UserDeserializer.class)
public class User implements Serializable {
// ...
}
I have included a bean.xml file in my WEB-INF folder with bean-discovery-mode
set to all
. What am I missing?