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";
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…