gpt4 book ai didi

java - @Inject 在 Struts 2 中的 Jersey REST web 服务中不起作用

转载 作者:行者123 更新时间:2023-11-30 08:20:33 25 4
gpt4 key购买 nike

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

我遇到如下所示的异常:

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)

有人能告诉我一些解决方案吗?

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";
}

}

最佳答案

来自official CDI Plugin documentation :

Use the right @Inject

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.

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

关于java - @Inject 在 Struts 2 中的 Jersey REST web 服务中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25975679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com