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

@Inject not working in Jersey REST webservices in Struts 2(@Inject 在 Struts 2 的 Jersey REST Web 服务中不起作用)

本文介绍了@Inject 在 Struts 2 的 Jersey REST Web 服务中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个用于测试 Struts 2 依赖注入 (@Inject) 的应用程序.除了我在其中定义了 Web 服务操作的 Jersey REST 服务类之外,注入在许多领域都运行良好.

I have created an application for testing the Struts 2 dependency injection (@Inject). The injection is working fine in many areas except Jersey REST service class within which I have defined the webservices actions.

我得到如下所示的异常:

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:

<?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:

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:

package net.viralpatel.struts2.action;

public class MoreServiceImpl implements MoreServices{

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

}

推荐答案

来自官方 CDI 插件文档:

使用正确的@Inject

Struts 2 和它的核心组件 XWork 使用它自己的内部依赖注入容器.有趣的是,你可以命名它JSR-330 的祖母,因为它是 Google 的早期预发布版本Guice 曾经由 Crazybob Lee 开发 - 同一个 Bob Lee,一起与 SpringSource 的 Rod Johnson 一起领导 JSR-330 规范.

Struts 2 and it's core component XWork use it's own internal dependency injection container. 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.

也就是说,你会发现 @Inject 注释都是com.opensymphony.xwork2.inject.Injectjavax.inject.Inject.不要混合这两个 - javax.inject.Inject 是您要使用的那个您的 Struts 2 CDI 插件和一般的 CDI 集成!当你也可以使用 Struts 的内部注解,效果可能是undefined 很奇怪 - 所以检查你的导入!

That said, you will find the @Inject annotation both as com.opensymphony.xwork2.inject.Inject and 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! While you could use Struts' internal annotation as well, the effect may be strange to undefined - so check your imports!

然后使用正确的 com.opensymphony.xwork2.inject.Inject
代替 javax.inject.Inject

这篇关于@Inject 在 Struts 2 的 Jersey REST Web 服务中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:@Inject 在 Struts 2 的 Jersey REST Web 服务中不起作用

基础教程推荐