问题描述
我有简单的休息WS
@Path("basic")
public class ServiceRS
{
private IServiceJAX service;
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String find(@FormParam("searchRequest") final String searchRequest)
{
//...
final List<Info> response = service.find(search);
//...
}
}
其中 IServiceJAX
是jax-webservice的 @Local
接口。
可以使用注释将 IServiceJAX
注入 ServiceRS
我不想要使用JNDI查找...
Where IServiceJAX
is @Local
interface of jax-webservice. Can I inject IServiceJAX
to ServiceRS
using annotation?
I don't want use JNDI lookup...
推荐答案
当然可以。虽然我想有其他的方法,我已经成功地运行一个简单的测试项目,一个 @Stateless
@WebService
, @Local
通过 @EJB
注释注入接口
将$ code> @Stateless 使用 @Path
注释的RESTFul Web服务。
Sure, you can. Although I suppose there are other ways, I have successfully run a simple test project with a @Stateless
@WebService
, @Local
implementation of an interface
, injected through @EJB
annotation into a @Stateless
RESTFul web service annotated with @Path
.
这不是你所要求的CDI注射,但它的效果很好,也可能适合你的需要。
This is not properly a CDI injection as you have demanded, but it works nicely and probably fits your needs anyway.
IServiceJAX 类:
public interface IServiceJAX {
public String hello(String txt);
}
IServiceJAXImpl 类:
@WebService(serviceName = "NewWebService")
@Local
@Stateless
public class IServiceJAXImpl implements IServiceJAX {
@WebMethod(operationName = "hello")
@Override
public String hello(@WebParam(name = "name") String txt) {
return "Hello " + txt + " !";
}
}
ServiceRS 类: / p>
ServiceRS class:
@Path("basic")
@Stateless
public class ServiceRS {
@EJB private IServiceJAX wsi;
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public String result(@PathParam("id") String id) {
return wsi.hello(id);
}
}
更新
如果您喜欢CDI注入,您可以保留上述代码,只需删除 @Local
和 @
注释。您可以使用以下方式注入此类的实例: IServiceJAXImpl
的无状态
If you prefer CDI injection, you can keep the above code and simply remove @Local
and @Stateless
annotations from IServiceJAXImpl
. You can inject an instance of this class using:
@Inject private IServiceJAX wsi;
而不是
@EJB private IServiceJAX wsi;
这篇关于依赖注射在休息的WS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!