I have created an application for testing the struts2 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
struts2.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";
}
}
From the official CDI Plugin documentation:
Then instead of
com.opensymphony.xwork2.inject.Inject
use the correct one:
javax.inject.Inject
For this particular question you should provide bean configuration for
The injection will work, but it's for internal use,
and you should consider the opportunities for other DI frameworks. See Dependency Injection.