RESTFul Service中如果要注入EJB实例,常规的@Inject将不起作用,在Jboss中,应用甚至都启动不起来(因为@Inject注入失败),解决方法很简单:将@Inject换成@EJB
参考代码:
CityInvoker是一个Stateless的EJB
package test; import javax.ejb.Stateless; import ... @Stateless public class CityInvoker { public CityResponse getCity() { CityResponse cityResponse = null; CityService cityService = ApplicationContextUtils.getCityService(); try { cityResponse = cityService.findCityByCode(RequestBuilder .buildCityFindRequest()); } catch (LMSException e) { e.printStackTrace(); } return cityResponse; }
下面是在RESTFul Service中注入的示例:
package test.rest; import javax.ejb.EJB; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.xml.bind.JAXBElement; import javax.xml.namespace.QName; @Path("/") public class TestService { final String XMLNS_NAMESPACE = "http://yjmyzz.cnblogs.com/rest/service"; final String ROOT_NODE = "root"; @EJB CityInvoker cityInvoker; @GET @Path("/findCity") @Produces(MediaType.APPLICATION_XML) public JAXBElement<CityDto> findCity() { JAXBElement<CityDto> result = new JAXBElement<CityDto>(new QName( XMLNS_NAMESPACE, ROOT_NODE), CityDto.class, cityInvoker .getCity().getCities().get(0)); return result; }
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦