Jetty 11 上的简单 Jersey 服务 运行

Simple Jersey service running on Jetty 11

几天来一直在与此作斗争,并试图在 Jetty 11 上启动一个简单的休息服务。我看到 war 已部署,但端点似乎并未处于活动状态。我什至无法在 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 启动。输出中没有错误。

HelloApplication.java

import jakarta.ws.rs.ApplicationPath;

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

HelloResource.java

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