在 Jetty 11 上运行的简单 Jersey 服务

schill32

已经为此奋斗了好几天,并试图在 Jetty 11 上启动一个简单的休息服务。我看到战争已部署,但端点似乎并未处于活动状态。我什至无法在 IntelliJ 中找到断点。帮助?

对 http://localhost:8080/jakartaee-sample-1.0-SNAPSHOT/api/hello-world 的请求失败并显示 404

为了部署,我使用 JRE 11、Jetty 11.0.3,以及模块 jmx、http、deploy、tsp、server。并通过 IntelliJ 启动。输出中没有错误。

你好应用程序

import jakarta.ws.rs.ApplicationPath;

@ApplicationPath("/api") public class HelloApplication {
    public HelloApplication() {
        System.console().printf("testing");
    } }

你好资源

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;

@Path("/hello-world")
public class HelloResource {
    @GET
    @Produces("text/plain")
    public String hello() {
        return "Hello, World!";
    }
}

pom.xml

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

    <groupId>com.example</groupId>
    <artifactId>jakartaee-sample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>jakartaee-sample</name>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-jetty-http</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>5.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
乔金·艾德费尔特

你需要做出的改变。

首先,应用声明需要修复...

import jakarta.ws.rs.ApplicationPath;

@ApplicationPath("/api") 
public class HelloApplication {

... 到 ...

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application; // new import

@ApplicationPath("api") // change this line
public class HelloApplication extends Application { // and this line

接下来,您的 pom.xml 文件中的依赖项。您需要使用通用的 servlet 容器,<packaging>war</packaging>并且需要将 servlet api 标记为<scope>provided</scope>.

  <dependencies>
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet</artifactId> <!-- changed -->
      <version>3.0.2</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.inject</groupId>
      <artifactId>jersey-hk2</artifactId>
      <version>3.0.2</version>
    </dependency>
    <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <version>5.0.0</version>
      <scope>provided</scope> <!-- changed -->
    </dependency>
  </dependencies>

最后,确保您${jetty.base}启用了“注释”模块。

$ cd /path/to/my-jetty-base
$ java -jar /path/to/jetty-home/start.jar --add-module=annotations
$ cp /path/to/my-code/example-ws.war webapps/
$ java -jar /path/to/jetty-home/start.jar

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

添加Jersey MOXy会使Jetty运行失败

以编程方式将Jersey REST服务附加到Jetty

Java:带有 Vaadin 11 框架的 Jetty 服务器

旧项目上的Jetty和Jersey 404错误

无法在Tomcat 9上运行Jersey 2 Restful Web服务

如何使用Jetty服务Angular应用程序,并与Jersey一起使用同一服务器?

使用Jersey在Java中运行简单的RESTful Web服务时的HTTP状态404

配置Jetty,Jersey和Guice

使业务域对象可用于嵌入式Jetty服务器中的Jersey Servlet上下文

关闭终端窗口后,Jetty服务器停止运行

如何运行Jetty服务器进行Java Junit测试

在局域网中运行Jetty Web服务器

在Docker上使用嵌入式Jetty服务器运行Java应用程序

Jetty服务器上的Famo.us

在Spark中的Jetty服务器上禁用密码套件

运行jersey 2 Web服务时我缺少什么?

使用码头9运行jersey 2服务器

Jersey REST服务上的用户身份验证

在Jersey rest服务中的post方法上使用Path Parameters

在Weblogic 12.1.1上使用Jersey 2.x Web服务

Tomcat上的Jersey REST服务出现404错误

将Jetty与JAX-RS-Jersey集成

如何在Glassfish 4上创建一个简单的JSON Jersey 2.x RESTful Web服务?

带有HK2定位器的嵌入式Jetty上的Jersey未加载ResourceConfig

简单的 MySQL 查询不在服务器上运行

Jersey Servlet 在 Tomcat 上运行?

gRPC服务器可以在其他HTTP / 2 Web服务器(如jetty / undertow / tomcat)上运行吗?

仅使用Jersey和Java创建简单的html服务器

使用jersey的Java(JAX-RS)中的简单RESTful Web服务,抛出HTTP状态404