简体   繁体   English

将Apache Jersey for REST Web服务与struts2 Web应用程序集成

[英]Integrate Apache Jersey for REST web services with a struts2 web apllication

I have a web application which has used struts2 framework. 我有一个使用struts2框架的Web应用程序。 I want to add support of REST web services to it using Apache Jersey. 我想使用Apache Jersey向其添加REST Web服务支持。 I have configured by application but I am getting 404 error while tring to run web services. 我已经按应用程序进行了配置,但是在尝试运行Web服务时却出现404错误。 I have searched on internet and found that filter intercepts all requests, making servlet mapping useless. 我在互联网上进行搜索,发现过滤器拦截了所有请求,从而使servlet映射无用。 I also found solution by excluding the url pattern by configuring struts.xml but still there is same problem of 404 not found. 我还通过配置struts.xml排除了URL模式,从而找到了解决方案,但仍然存在未找到404的相同问题。 I am attaching my code below. 我在下面附上我的代码。 Thanks in advance. 提前致谢。

web.xml web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>ocmsLatest</display-name>
  <context-param>
    <param-name>quartz:config-file</param-name>
    <param-value>/quartz.properties</param-value>
  </context-param>
  <context-param>
    <param-name>quartz:shutdown-on-unload</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <param-name>quartz:wait-on-shutdown</param-name>
    <param-value>false</param-value>
  </context-param>
  <context-param>
    <param-name>quartz:start-scheduler-on-load</param-name>
    <param-value>true</param-value>
  </context-param>
  <listener>
    <listener-class>
             org.quartz.ee.servlet.QuartzInitializerListener
         </listener-class>
  </listener>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <context-param>
    <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
    <param-value>/WEB-INF/tiles.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
  </listener>
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
  <welcome-file-list>
    <welcome-file>/pages/user/index.jsp</welcome-file>
  </welcome-file-list>
  <jsp-config>
    <jsp-property-group>
      <url-pattern>*.jsp</url-pattern>
      <trim-directive-whitespaces>true</trim-directive-whitespaces>
    </jsp-property-group>
  </jsp-config>
  <servlet>
    <description></description>
    <display-name>InitServlet</display-name>
    <servlet-name>InitServlet</servlet-name>
    <servlet-class>com.ocms.util.InitServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>InitServlet</servlet-name>
    <url-pattern>/InitServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>Captcha</servlet-name>
    <servlet-class>com.roseindiaCaptcha.servlet.RoseIndiaCaptcha</servlet-class>
    <init-param>
      <description>passing height</description>
      <param-name>height</param-name>
      <param-value>60</param-value>
    </init-param>
    <init-param>
      <description>passing width</description>
      <param-name>width</param-name>
      <param-value>200</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>Captcha</servlet-name>
    <url-pattern>/Captcha.jpg</url-pattern>
  </servlet-mapping>
  <error-page>
    <error-code>404</error-code>
    <location>/pages/error404.jsp</location>
  </error-page>
    <servlet>
  <servlet-name>Jersey REST Service</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.ocms.rest</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>Jersey REST Service</servlet-name>
  <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

struts.xml struts.xml

<struts>
    <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="com.ocms.app.resources.Resources" />
    <constant name="struts.multipart.maxSize" value="104857600000" />
//all action mappings
</struts> 

Hello.java 你好

package com.ocms.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("/hello")
public class Hello {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello() {
        return "Hello Jersey";
    }
}

我将FilterDispatcher, org.apache.struts2.dispatcher.FilterDispatcher更改为org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter ,然后解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM