简体   繁体   English

@Inject 在 Struts 2 中的 Jersey REST 网络服务中不起作用

[英]@Inject not working in Jersey REST webservices in Struts 2

I have created an application for testing the Struts 2 dependency injection ( @Inject ).我创建了一个用于测试 Struts 2 依赖项注入 ( @Inject ) 的应用程序。 The injection is working fine in many areas except Jersey REST service class within which I have defined the webservices actions.除了我在其中定义了 webservices 操作的 Jersey REST 服务类之外,注入在许多领域都运行良好。

I am getting exception like as shown below:我收到如下所示的异常:

Sep 22, 2014 8:48:50 AM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException
    at usermodules.services.UserModulesServices.userName(UserModulesServices.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

Can anyone please tell me some solution for this?任何人都可以告诉我一些解决方案吗?

struts.xml: struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
<bean class="net.viralpatel.struts2.action.MoreServiceImpl" name="services" />

  <constant name="struts.action.excludePattern" value="/rest/.*" />
    <constant name="struts.enable.DynamicMethodInvocation"
        value="false" />
    <constant name="struts.devMode" value="false" />
    <constant name="struts.custom.i18n.resources"
        value="ApplicationResources" />
  
 
    <package name="default" extends="struts-default" namespace="/">
        <action name="login"
            class="net.viralpatel.struts2.action.LoginAction">
            <result name="success">Welcome.jsp</result>
            <result name="error">Login.jsp</result>
        </action>
    </package>
</struts>

UserModulesServices.java: UserModulesServices.java:

import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import net.viralpatel.struts2.action.MoreServiceImpl;

import com.opensymphony.xwork2.inject.Inject;

@Path("/users")
public class UserModulesServices {
    
    @Inject("services")
    public MoreServiceImpl moreServiceImpl;

    public MoreServiceImpl getMoreServiceImpl() {
        return moreServiceImpl;
    }

    public void setMoreServiceImpl(MoreServiceImpl moreServiceImpl) {
        this.moreServiceImpl = moreServiceImpl;
    }
    
    @GET
    @Path("/name/{i}")
    @Produces(MediaType.TEXT_PLAIN)
    public String userName(@PathParam("i") String i) {
        System.out.println("name::::::::" + moreServiceImpl.validate());
        return "{\"name\":\"" + i + "\"}";
    }
}

MoreServiceImpl.java: MoreServiceImpl.java:

package net.viralpatel.struts2.action;

public class MoreServiceImpl implements MoreServices{

    @Override
    public String validate() {
        return "testing";
    }

}

From the official CDI Plugin documentation :来自官方 CDI 插件文档

Use the right @Inject使用正确的@Inject

Struts 2 and it's core component XWork use it's own internal dependency injection container. Struts 2 及其核心组件 XWork 使用它自己的内部依赖注入容器。 Interestingly, you could name it JSR-330's grandma, since it is an early pre-release version of Google Guice once developed by Crazybob Lee - the same Bob Lee that, together with SpringSource's Rod Johnson, lead the JSR-330 specification.有趣的是,您可以将其命名为 JSR-330 的祖母,因为它是曾经由 Crazybob Lee 开发的 Google Guice 的早期预发布版本 - 同一个 Bob Lee 与 SpringSource 的 Rod Johnson 一起领导 JSR-330 规范。

That said, you will find the @Inject annotation both as com.opensymphony.xwork2.inject.Inject and javax.inject.Inject .也就是说,您会发现@Inject注释既是com.opensymphony.xwork2.inject.Inject又是javax.inject.Inject Don't mix up those two - javax.inject.Inject is the one you want to use with your Struts 2 CDI plugin and CDI integration in general !不要混淆这两个 - javax.inject.Inject是您想要与 Struts 2 CDI 插件和 CDI 集成一起使用的一般 While you could use Struts' internal annotation as well, the effect may be strange to undefined - so check your imports!虽然您也可以使用 Struts 的内部注释,但效果可能对未定义很奇怪 - 所以请检查您的导入!

Then instead of com.opensymphony.xwork2.inject.Inject然后代替com.opensymphony.xwork2.inject.Inject
use the correct one: javax.inject.Inject使用正确的: javax.inject.Inject

For this particular question you should provide bean configuration for对于这个特定问题,您应该提供 bean 配置

<bean class="net.viralpatel.struts2.action.UserModulesServices" name="userModulesServices" />

The injection will work, but it's for internal use,注射会起作用,但它是供内部使用的,

Internally, the framework uses its own dependency injection container that is very similar to Google Guice.在内部,该框架使用自己的依赖注入容器,这与 Google Guice 非常相似。

and you should consider the opportunities for other DI frameworks.你应该考虑其他 DI 框架的机会。 See Dependency Injection .请参阅依赖注入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM