java - 嵌入式 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)
}
}
}
解决方案
推荐阅读
- excel - 基于 COUNTA 公式选择单元格的 VBA 代码
- c# - 如何将 byte[] 转换为位图
- javascript - 无法在字符串“”上创建属性“ignore_whitespace”
- c - 分段错误:调用词干函数时为 11
- vue.js - 在vue中创建一个路由出路由器
- flutter - image_picker 在哪里存储图像?
- python - Keras Lambda 层执行最大值并以 (?, 1) 形状输出
- php - 在数据表中,sProcessing 文本未加载
- c# - 如何对某些 DbSet 使用 Skip() 和 Take()
的属性是 ICollection<> - r - 如何优化批量预测