首页 > 解决方案 > 嵌入式 tomcat 中的 JAX-RS (Jersey) webapp 在 IDE 中工作,但不能作为 JAR

问题描述

我正在构建一个 jersey webapp,并且我正在使用嵌入式 tomcat 来实现可移植性。使用单一资源非常简单。我遇到的问题是,当我通过 IDE (intellij) 运行 webapp 时,一切正常,我看到我的类扩展ResourceConfig正在初始化。它还响应 HTTP 请求。它与 IDE 一起使用 Gradle 和内部构建工具

但是,一旦我通过命令行使用 gradle 构建它,使用 shadowJar 插件,tomcat 中没有加载任何内容。我看到它在旋转,但没有任何日志行出现,并且我的所有 HTTP 请求都导致 404。我不确定如何调试它。

我的主课

public class Main {

    public static void main(String[] args) {
        new Main().start();
    }

    public void start() {

        String port = System.getProperty("port");
        if (port == null) {
            port = "8442";
        }

        Tomcat tomcat = new Tomcat();
        tomcat.setPort(Integer.parseInt(port));
        tomcat.getConnector();

        String absolutePath = new File(".").getAbsolutePath();
        Context context = tomcat.addWebapp("/webapp", absolutePath);

        tomcat.start();
        tomcat.getServer().await();
    }
}

我的MyApplication班级

@ApplicationPath("app")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        setApplicationName("My web app");
        LOG.info("Hello!")
        packages("com.xxx.xxx.xxx.resources");
    }
}
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'
buildscript {
    dependencies {
        classpath fileTree(dir: '../libs/shadow/', include: '*.jar')
    }
}
mainClassName = 'com.xxx.xxx.xxx.Main'
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

dependencies {
    implementation libraries.javax_servlet
    implementation libraries.args4j
    implementation libraries.log4j2
    implementation libraries.common_loggings
    implementation libraries.jersey
    implementation libraries.tomcat

    implementation project(":common")

}


jar {
    manifest {
        attributes('Main-Class': 'com.xxx.xxx.xxx.Main')
    }

    from {
        configurations.runtimeClasspath.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
}

标签: javatomcatjerseyjax-rs

解决方案


推荐阅读