从头开始没有任何以前的Jersey 1.x知识,我很难理解如何在我的Jersey 2.0项目中设置依赖注入.
我也明白HK2可用于Jersey 2.0,但我似乎无法找到有助于Jersey 2.0集成的文档.
@ManagedBean
@Path("myresource")
public class MyResource {
@Inject
MyService myService;
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/getit")
public String getIt() {
return "Got it {" + myService + "}";
}
}
@Resource
@ManagedBean
public class MyService {
void serviceCall() {
System.out.print("Service calls");
}
}
Run Code Online (Sandbox Code Playgroud)
的pom.xml …
我目前正在开发一个应用程序,其中Jersey JAX-RS作为后端,AngularJS作为前端; 我需要一种身份验证,因此对于每个请求,我都会发送一个应该由后端验证的令牌.为此,我决定创建一个Jersey过滤器来查找该令牌,然后调用我的AuthenticateService来检查用户是否可以进行身份验证.然后通过@RolesAllowed注释管理授权.
这是我的问题:我无法在Jersey过滤器中注入EJB,因为它可以很好地处理资源.但是使用过滤器,服务始终保持为null
知道怎么骗它吗?谢谢
过滤代码:
@Provider
@Priority( Priorities.AUTHORIZATION )
public class AuthenticationFilter implements ContainerRequestFilter {
@EJB( name=AuthenticationService.LOOKUP_NAME)
private AuthenticationService authService;
@Override
public void filter( ContainerRequestContext requestContext ) throws IOException {
/**
* Get headers parameters
*/
String userIdStr = requestContext.getHeaderString( SecurityConsts.HEADER_ID_PARAMETER );
int userId = 0;
if( userIdStr != null && !userIdStr.isEmpty() ) {
userId = Integer.parseInt( userIdStr );
}
String securityToken = requestContext.getHeaderString( SecurityConsts.HEADER_TOKEN );
User user = null;
/**
* If a token is …Run Code Online (Sandbox Code Playgroud)