@Context注入JAX-RS使用的无状态EJB [英] @Context injection in Stateless EJB used by JAX-RS

查看:115
本文介绍了@Context注入JAX-RS使用的无状态EJB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面这样的设置。这是一个简化版本,但我认为它是基本的想法。我使用的是Jersey 2.16,Java 1.8和Glassfish Open Source 4.1

  public interface IReportService {
String addNewReport报告);
}

@Path(reports)
public class ReportResource实现IReportService {
/ **
*服务层。
* /
@EJB
私有临时ReportService服务;

@POST
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Produces(MediaType.TEXT_PLAIN)
@Override
public String addNewReport(最终报告报告){
return service.addNewReport(report);
}
}

@Stateless
@LocalBean
public class ReportService实现IReportService {

@EJB
私人IReportPersistence reportPersistence;

@Context
SecurityContext secContext;

public String addNewReport(final Report report){
report.setUserName(secContext.getUserPrincipal()。getName());
reportPersistence.persist(report);
}
}

但是当我部署并尝试点击网络服务我从安全上下文得到一个NullPointer异常。看来,上下文没有被注入。我检查并且它是secContext变量本身,而不只是从getUserPrincipal()返回null。除了NullPointer之外,Glassfish日志中没有任何警告或异常(导致返回给Web客户端的错误为500)。

解决方案

p>问题是您在错误的地方使用 SecurityContext 。您必须在REST资源类中使用它。



您可以尝试以下操作:

 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $((((((((((((((((((((((((((((((((((((((((((((((((最终报告报告,@Context SecurityContext sc){
report.setUserName(sC.getUserPrincipal()。getName());
return service.addNewReport(report);
}

有关更多详细信息,请参阅泽西文档 - 第16章安全性



EJB必须使用 EJBContext (或 SessionContext )。


I have something like this setup below. This is a simplified version but I think it gets the basic idea across. I am using Jersey 2.16, Java 1.8, and Glassfish Open Source 4.1

public interface IReportService {
    String addNewReport(Report report);
}

@Path("reports")
public class ReportResource implements IReportService {
    /**
    * Service layer.
    */
    @EJB
    private transient ReportService service;

    @POST
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    @Produces(MediaType.TEXT_PLAIN)
    @Override
    public String addNewReport(final Report report) {
       return service.addNewReport(report);
    }
}

@Stateless
@LocalBean
public class ReportService implements IReportService {

   @EJB
   private IReportPersistence reportPersistence;

   @Context
   SecurityContext secContext;

   public String addNewReport(final Report report) {
       report.setUserName(secContext.getUserPrincipal().getName());
       reportPersistence.persist(report);
    }
}

But when I deploy and try to hit the web-service I get a NullPointer exception from the security context. It just seems that the Context is not being injected at all. I checked and it's the secContext variable itself, not just the return from getUserPrincipal() that is null. There are no warning or exceptions in the Glassfish log besides my NullPointer (which results in a 500 error returned to the web client).

解决方案

The problem is that you are using the SecurityContext in the wrong place. You have to use it inside your REST resource class.

You can try the following:

@POST
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces(MediaType.TEXT_PLAIN)
@Override
public String addNewReport(final Report report, @Context SecurityContext sc) {
   report.setUserName(sC.getUserPrincipal().getName());
   return service.addNewReport(report);
}

For more details have a look at the Jersey Documentation - Chapter 16. Security.

Inside of EJBs you have to use the EJBContext (or the SessionContext).

这篇关于@Context注入JAX-RS使用的无状态EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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