Jersey with Struts2 [duplicate]

2019-01-05 06:06发布

This question already has an answer here:

I am using jersey with Struts2. But by RestFul Service calls are not working. Below are my configurations files

struts.xml

<?xml version="1.0" encoding="UTF-8"?>    
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
        "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>
    <package name="default" namespace="/" extends="struts-default">
    <result-types>
        <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
    </result-types>

     <interceptors>
            <interceptor-stack name="default">
                 <interceptor-ref name="defaultStack">
                    <param name="exception.logEnabled">true</param>
                    <param name="exception.logLevel">ERROR</param>
                    <param name="params.excludeParams">/service/*</param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="default" />

        <action name="defaultAction"
            class="com.gemini.web.controller.BinMasterController"
            method="binMaster">
            <result name="binMaster" type="tiles">binMaster</result>
        </action>    
    </package>    
</struts>

Web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <!-- Client config -->
    <context-param>
        <param-name>
            org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
        </param-name>
        <param-value>
            /WEB-INF/tiles.xml
        </param-value>
    </context-param>

    <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>

    <listener>
        <listener-class>
            org.apache.struts2.tiles.StrutsTilesListener
        </listener-class>
    </listener>

    <!-- Jersey Support Configuration for RestFul WebServices -->
    <servlet>
        <servlet-name>jersey-serlvet</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>com.gemini.rest.controller</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/service/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>    
</web-app>

2条回答
Juvenile、少年°
2楼-- · 2019-01-05 06:45

I have a similar architecture (using Struts2 and Jersey).

In my web.xml I have:

<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>/admin/*</url-pattern>
</filter-mapping>

My Jersey is configured through an extended ResourceConfig:

@ApplicationPath("rest")
public class Config extends ResourceConfig {

    public Config() {
        super();
        register(new Binder());
        packages(true, this.getClass().getPackage().getName());
    }

}

So the two are on separate url paths:

Struts is on /admin and Jersey is on /rest.

查看更多
做个烂人
3楼-- · 2019-01-05 06:53

With this configuration Struts2 filter intercepts all requests.

To exclude your /service/* requests use struts.action.excludePattern constant:

<constant name="struts.action.excludePattern" value="/service/.*" />
查看更多
登录 后发表回答