Jersey REST Webservice is not working in Struts 2

2019-03-22 16:37发布

问题:

I have created an application Struts2 with Jersey REST webservice, the Struts2 application is working fine but the rest webservice is not working, when execute the URL

http://localhost:8080/SAMPLE/resources/users/name/manu

I am getting

There is no Action mapped for namespace /resources/users/name and action name manu. - [unknown location]

Does the version of Struts causes any issues, since I am using Struts 2.1.6 ?

Can anyone please tell me some solution for this

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>ServletAdaptor</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>otims.usermodules.services</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
             30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list> 
  </web-app>

struts.xml:

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.action.excludePattern" value="/resources/.*" />
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.custom.i18n.resources" value="com.ocms.app.resources.Resources" />
    <constant name="struts.multipart.maxSize" value="104857600000" />
    <bean class="otims.usermodules.dao.UserModulesServicesImpl" name="modules"></bean>
    <constant name="struts.serve.static" value="true" />
    <constant name="struts.serve.static.browserCache" value="false" />
    <package name="default" extends="struts-default"  namespace="/">
:
:
:

UserModulesServices.java:

package otims.usermodules.services;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/users")
public class UserModulesServices {
    @GET
    @Path("/name/{i}")
    @Produces(MediaType.TEXT_PLAIN)
    public String userName(@PathParam("i") String i) {
        System.out.println("name::::::::" + i);
        return "{\"name\":\"" + i + "\"}";
    }
}

回答1:

does the version of struts2 causes any issues, since i am using 2.1.6 ?

Yes, it does. excludePattern has been introduced in... 2.1.7.

Please consider upgrading (due to a lot of improvement and security fixes) to the latest Struts version (2.3.16.3 today, 2.3.18 soon).

With 2.3.18, you will also be able to use the new JakartaStreamMultiPartRequest, and try dangerous stuff like setting 100 GigaBytes of Request size as you are doing now :)