(泽西岛)一旦应用程序构建,就无法找到REST资源

时间:2014-06-07 00:34:55

标签: spring rest maven jboss jersey

在部署Jersey REST应用程序时,我遇到了一个相当奇怪的问题。

当我从Eclipse运行JBoss(7.2.0)并将项目部署到它(即将项目拖放到Eclipse中的Server)时,我可以访问REST资源(使用@Path注释)。但是,如果我将相同的项目构建到WAR(通过maven)并将其部署到同一个Jboss中(即将WAR文件移动到jboss' deploy文件夹中),我最终得到404"请求的资源是不可用。"错误。

在这两种情况下,我使用的是相同的项目,jboss和请求路径,因此我不明白这里可能出现的问题。

有人看起来像这样吗?我也怀疑它可能与我将Jersey(2.8)与Spring(4.0.4)整合在一起的事实有关。

我提前感谢您的帮助。

这是我的设置:

的pom.xml:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>


  <groupId>org.test.api</groupId>
  <artifactId>rest-api</artifactId>
  <version>0.0.0.0.0.0-SNAPSHOT</version>
  <packaging>war</packaging>


    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.0.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.0.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet</artifactId>
      <version>2.8</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-multipart</artifactId>
      <version>2.8</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.ext</groupId>
      <artifactId>jersey-spring3</artifactId>
      <version>2.8</version>
      <scope>compile</scope>

      <!-- Since I'm using Spring 4, I have to exclude these -->
      <exclusions>
        <exclusion>
          <artifactId>spring-core</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-web</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-beans</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
</project>

的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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    <display-name>REST-API</display-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextClass</param-name>
        <param-value> 
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext 
        </param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>org.test.api</param-value>
    </context-param>

    <servlet>
        <servlet-name>REST-API</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.tracing.type</param-name>
            <param-value>ALL</param-value>
        </init-param>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>org.test.api.RestApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>REST-API</servlet-name>
        <url-pattern>/v1/*</url-pattern>
    </servlet-mapping>
</web-app>

RestApplication.java:

package org.test.api;

import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;

public class RestApplication extends ResourceConfig {

    public RestApplication() {
        register(RequestContextFilter.class);

        register(MultiPartFeature.class);

        packages("org.test.api.resources");

    }
}

TestResource.java

package org.test.api.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/test")
public class TestResource {

    @GET
    public String hello() {
        return "Sup";
    }
}

RestSpringBeans

package org.test.api;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(SomeOtherBeans.class)
public class MediaManagementBeans {

}

请求网址:http://localhost:8080/rest-api/v1/test

  • 失败:如果部署为maven制造的WAR

0 个答案:

没有答案