java - Injection of stateless session bean into custom JsonDeserializer fails -
i building application providing jax-rs rest service using jpa (eclipselink). when exposing user entities on json, using @xmltransient
annotation on fields (e.g. password field) hide them json representation. when sending create or update (post/put) operation, populate missing fields again jpa correctly perform operation.
my current approach have custom jsondeserializer
used deserialize user , add missing fields. inject (using @inject
) userfacaderest
bean handles jpa-stuff. however, injection fails , bean instance null
(which of course causes nullpointerexception
).
my userfacaderest
bean annoted 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); // line produces nullpointerexception return user; } }
which use on user entity @jsondeserialize
:
@entity @table(name = "user") @xmlrootelement @jsondeserialize(using = userdeserializer.class) public class user implements serializable { // ... }
i have included bean.xml file in web-inf folder bean-discovery-mode
set all
. missing?
jon peterson pointed me right direction. chose implement 'hackish' solution, in way. please note there 2 options here (if know one, please let me know!). short version:
- hackish solution (the solution chose): inject bean programmatically using
javax.enterprise.inject.spi.cdi.current().select(userfacaderest.class).get()
described in accepted answer of question mentioned jon or - better (clean) solution (but more elaborate): redesign logic fill missing fields after deserialization suggested jon.
so question, solution looks follows:
1.
import javax.enterprise.inject.spi.cdi; public class userdeserializer extends jsondeserializer<user> { private final userfacaderest userfacade = cdi.current().select(userfacaderest.class).get(); // rest before }
2. in case, in deserialize
method of jsondeserializer
construct user holds userid. in every request method have examine users , replace them actual user calling entitymanager.find(user.class, user.getuserid())
. means more effort in business logic have keep in mind everytime need work on user
in request method, first have query 'full' user
object. in first solution, query hidden business logic , happens in jsondeserializer
.
public class userdeserializer extends jsondeserializer<user> { @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(); return new user(userid); // placeholder user object containing user id, needs replaced in business logic } }
Comments
Post a Comment