依赖注射在休息的WS [英] Dependency injection in restful WS

查看:128
本文介绍了依赖注射在休息的WS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的休息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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆