java - Performing a JNDI lookup in a JAXB XmlAdapter -
my project java enterprise project , consists of 3 modules:
- assembly (ear)
- ejb (jar)
- web (war)
my domain model resides in ejb. includes manufacturer
class , model
class. one-to-many relationship exists between two. expose instances of these manufacturers , models through rest interface resides in web project.
whenever access 1 of these manufacturers, following xml-code generated:
<manufacturer id=1> <name>ford</name> <models> <model id=1> <name>fiesta</name> </model> <model id=2> <name>focus</name> </model> </models> </manufacturer>
however, want this:
<manufacturer id=1> <name>ford</name> <models> <model>1</model> <model>2</model> </models> </manufacturer>
i have achieved desired effect writing specialized xmladapter
, modeladapter
, annotate field in manufacturer
class @xmljavatypeadapter(modeladapter.class)
. adapter resides in ejb module well. problem arises, however, when model
needs unmarshalled:
private modelfacade modelfacade; @override public model unmarshal(long id) throws exception { return modelfacade.find(id); }
the modelfacade
, stateless session bean, can not injected xmladapter
, unmarshalling process therefore fail.
i have been advised write messagebodyreader
in order able "manually" instantiate adapter , pass facade argument specialized message body reader need implemented in web module. contain behaviour in ejb module simple reason if ever decide create, instance, desktop application depends on ejb, don't need deal same issue again.
in order achieve behaviour, can perform jndi lookup in constructor of adapter:
public abstractadapter(string name) throws namingexception { facade = (abstractfacade<b>) lookup("java:app/myejb/" + name); } private object lookup(string name) throws namingexception { context c = new initialcontext(); return c.lookup(name); }
and work fine, not sure right way go. doing jndi lookup ejb module fine solution or there more favourable one?
you specify parameter of type java.io.inputstream
on jax-rs method (which on session bean). unmarshal inputstream
leveraging jaxb. give opportunity configure javax.xml.bind.unmarshaller
appropriate xmladapter
.
for more information
Comments
Post a Comment