Struts2 @Inject doesn't work in Jersey. Other web services

I have created an application to test struts2 ( @Inject

) dependency injection . Injection works fine in many areas, apart from the jersey service class, in which I defined the web service actions.

I am getting an exception like 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 someone 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";
    }

}

      

+2


source to share


2 answers


From the official documentation on connecting CDI :

Use correct @Inject

Struts 2 and its core component XWork use its own internal injection container. Interestingly, you could call it JSR-330 granny, as this is an early preview of Google Guice, once developed by Crazybob Lee - the same Bob Lee who, along with SpringSource Rod Johnson, spearheads the JSR-330 specification.

Thus, you will find annotation in @Inject

both the view com.opensymphony.xwork2.inject.Inject

and javax.inject.Inject

. Don't mix the two - the javax.inject.Inject

one you want to use with your Struts 2 CDI plugin and CDI integration in general
! While you can also use internal Struts annotation, the effect can be strange for undefined - so check your imports!



Then com.opensymphony.xwork2.inject.Inject


use the correct one instead :javax.inject.Inject

+1


source


For this specific question, you must provide a bean config for

<bean class="net.viralpatel.struts2.action.UserModulesServices" name="userModulesServices" />

      

The injection will work, but it is for internal use,



Internally, the framework uses its own dependency injection container, which is very similar to Google Guice.

and you should consider the possibilities for other DI frameworks. See Dependency Injection .

+1


source







All Articles