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

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

问题描述

我已经创建了一个用于测试Struts 2依赖项注入的应用程序( @Inject ).除了在我定义了webservices操作的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

Use the right @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.Inject javax.inject.Inject .不要混合这两个- javax.inject.Inject 是您要使用的那个一般而言,您的Struts 2 CDI插件和CDI集成!当你也可以使用Struts的内部注释对于未定义很奇怪-请检查您的导入!

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

Then instead of com.opensymphony.xwork2.inject.Inject
use the correct one: javax.inject.Inject

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

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