【问题标题】:Running a gradle jersey 2.0 web app without a web.xml in eclipse在 Eclipse 中运行没有 web.xml 的 gradle jersey 2.0 Web 应用程序
【发布时间】:2014-05-27 07:14:58
【问题描述】:

我有一个在 Eclipse 中导入的 gradle 项目。该项目是一个 Web 应用程序,我设法将其构建并部署到 tomcat,但没有加载球衣 REST 服务,因此在尝试访问这些服务时出现 404。

我正在使用支持 servlet-api 3.0 的 tomcat7。

请注意,我没有使用 web.xml。我有一个扩展应用程序的类,应该加载正确的类并为 REST 服务设置根。 这是我的课:

import java.util.Set;
import javax.ws.rs.core.Application;

@javax.ws.rs.ApplicationPath("api")
public class JerseyApplicationConfig extends Application
{
    @Override
    public Set<Class<?>> getClasses()
    {    
        Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
        addRestResourceClasses(resources);
        return resources;
    }

    private void addRestResourceClasses(Set<Class<?>> resources)
    {
        resources.add(com.cisco.gemini.LoggingRESTService.class);
    }    
}

问题是这个类不是由 jersey 创建的,因此没有设置 REST 服务。

谢谢, 加尔

更新:

我设法解决了这个问题,所以我在这里发布解决方案以供大家受益。 这是我的 gradle 文件,它可以让事情正常工作。我现在可以从 Eclipse 在 tomcat7 上构建和运行服务,并且 jersey servlet 可以正确加载。

注意:我还希望能够从命令行运行服务(出于测试目的),因此我应用了 jettyEclipse 插件。 gradle 自带的 jetty 插件不支持 servlet-api 3.0。这对我不起作用(因为我正在使用的 slf4j 存在一些冲突),但它可能对其他人有用。

谢谢, 加尔

apply plugin: 'java' 
apply plugin: 'war' 
apply plugin: 'jetty' 
apply plugin: 'eclipse-wtp'

repositories {
  mavenCentral()
}

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath (group: 'com.sahlbach.gradle', name: 'gradle-jetty-eclipse-plugin', version: '1.9.+')
    }
}
apply plugin: 'jettyEclipse'

dependencies {
  compile 'javax.servlet:javax.servlet-api:3.0.1'
  compile 'com.sun.jersey:jersey-server:1.18.1'
  compile 'com.sun.jersey:jersey-core:1.18.1'
  compile 'com.sun.jersey:jersey-json:1.18.1'
  compile 'com.sun.jersey:jersey-servlet:1.18.1'  
  compile 'javax.ws.rs:jsr311-api:1.1.1'
  compile fileTree(dir: 'libs', include: '*.jar')    
  compile 'org.codehaus.jettison:jettison:1.3.5'
}

eclipse {
   wtp {
     component {
       contextPath = 'MyService'
       deployName = 'MyService'      
        facet {
          facet name: 'jst.web', version: '3.0'
          facet name: 'java', version: '1.7'        }
     }
   }
 }

【问题讨论】:

    标签: eclipse gradle jetty servlet-3.0 jersey-2.0


    【解决方案1】:

    如果你想让它从命令行运行,你可以尝试使用 gretty 插件 - https://github.com/akhikhl/gretty - 它支持更新版本的 Jetty,包括 Servlet 3.0 兼容版本。或者另一种方法是让你的应用程序兼容 2.5:添加 web.xml 并在此处定义各种组件。我更喜欢第一种解决方案。

    【讨论】:

      猜你喜欢
      • 2014-11-19
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多