【问题标题】:Implement RESTful service in Java EE using J2EE Preview runtime in Eclipse在 Eclipse 中使用 J2EE Preview 运行时在 Java EE 中实现 RESTful 服务
【发布时间】:2020-09-07 17:40:36
【问题描述】:

这些是我在 Eclipse IDE for Java EE 中使用 Jax-RS 创建一个简单的 RESTful Web 服务所遵循的步骤。

  • 创建一个新的动态 Web 项目(名称:TestExample)
    • 选择目标运行时作为 J2EE Preview
    • 动态网页模块版本:v3.1
    • 配置类型:具有以下项目方面的自定义
      • 动态网络模块:v3.1
      • Java : v1.8
      • JAX-RS(REST Web 服务):v2.0
    • 勾选“生成 web.xml 部署描述符”
  • 在 Java 资源下(在 Project Explorer 中)创建一个新包 (my.test.example) 和一个类 (TestService)
  • 导入外部jar文件javax.ws.rs-api-2.0.jar并将其添加到构建路径以解决javax.ws.rs导入错误
  • TestService.java

    package my.test.example;
    
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.core.Application;
    
    @Path("/MyTestService")
    @ApplicationPath("/resources")
    public class TestService extends Application {
    
        // http://localhost:8080/TestExample/resources/MyTestService/sayHello
        @GET
        @Path("/sayHello")
        public String getHelloMsg() {
            return "Hello World";
        }
    }
    
  • web.xml
        <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>TestExample</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    
  • 运行项目

在浏览器中打开这个网址:http://localhost:8080/TestExample/resources/MyTestService/sayHello 会返回这个:

HTTP ERROR 404 Not Found
URI:    /TestExample/resources/MyTestService/sayHello
STATUS: 404
MESSAGE:    Not Found
SERVLET:    default

控制台输出

Starting preview server on port 8080

Modules:
  TestExample (/TestExample)

2020-05-21 11:45:45.175:INFO::main: Logging initialized @1815ms to org.eclipse.jetty.util.log.StdErrLog
2020-05-21 11:45:45.894:INFO:oejs.Server:main: jetty-9.4.27.v20200227; built: 2020-03-02T14:40:42.212Z; git: a304fd9f351f337e7c0e2a7c28878dd536149c6c; jvm 1.8.0_171-b11
2020-05-21 11:45:48.219:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /TestExample, did not find org.eclipse.jetty.jsp.JettyJspServlet
2020-05-21 11:45:48.289:INFO:oejs.session:main: DefaultSessionIdManager workerName=node0
2020-05-21 11:45:48.289:INFO:oejs.session:main: No SessionScavenger set, using defaults
2020-05-21 11:45:48.299:INFO:oejs.session:main: node0 Scavenging every 600000ms
2020-05-21 11:45:48.425:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@7d907bac{TestExample,/TestExample,file:///C:/.../wip/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/TestExample/,AVAILABLE}{C:/.../wip/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/TestExample}
2020-05-21 11:45:48.489:INFO:oejs.AbstractConnector:main: Started ServerConnector@6ed3ef1{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
2020-05-21 11:45:48.504:INFO:oejs.Server:main: Started @5150ms

预期的输出是Hello World。我在这里想念什么?

以下步骤基于 Youtube 上的教程:Java EE Tutorial #18 - RESTful Web Services with Jax-RS

Eclipse 版本:2020-03 (4.15.0)

注意:我让它与 GlassFish Runtime 一起使用,但仍然想知道它为什么不能与 J2EE Runtime 一起使用。

Eclipse 中的项目结构:

【问题讨论】:

  • 试试这个网址一次:打开这个网址:localhost:8080/resources/MyRestService/sayHello
  • @SanthoshKThadka 它返回Error 404 - Not Found No context on this server matched or handled this request.,我想我在 web.xml 中缺少资源的 serverlet 映射,但我不确定
  • @SanthoshKThadka 这没有帮助
  • 我不认为你的服务和应用程序应该是同一个类。你能把它们分开吗? (确保该服务位于您的应用程序所在包的同一个包或子包中)

标签: java eclipse rest jakarta-ee jax-rs


【解决方案1】:

从项目结构中可以看出,J2EE Preview 运行时不包括其自己的 JAX-RS 实现。但是,在 GlassFish Runtime 中已经包含了 JAX-RS 的参考实现。

我执行了以下操作以使其与 J2EE Preview 运行时一起工作:

不做这一步

导入外部jar文件javax.ws.rs-api-2.0.jar并添加到build javax.ws.rs导入错误的解决路径

我将eclipse项目转换为maven项目并在pom.xml添加了以下依赖1

<!-- v2.25.1 conforms to JAX-RS v2.0 spec -->
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.25.1</version>
</dependency>

web.xml2,我添加了这个

<servlet>
    <servlet-name>REST Service</servlet-name>
    <!-- Define servlet class to be used-->
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <!-- Configuring Jersey container Servlet or Filter to use package scanning-->
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>my.test.example</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <!-- Map servlet to URL with pattern "/resources/*"-->
    <servlet-name>REST Service</servlet-name>
    <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

注意

here 所述,Servlet 3.0 及更高版本 web.xml 是可选的。可以改用注释。但是,正如在这个问题中看到的,Why won't the preview server in Eclipse respect my @WebServlet annotation? 注释似乎不适用于 J2EE 运行时,因此使用了web.xml

参考文献

1Jersey Dependencies for Servlet based server-side application

2Servlet-based Deployment , How to deploy a JAX-RS application?

【讨论】:

    【解决方案2】:

    快速

    • @ApplicationPath("/resources") 中删除/
    • web.xml 不是必需的

    说明

    根据此示例:

    @ApplicationPath() 的参数是一个简单的词,没有/

    尝试:

    @Path("/MyTestService")
    @ApplicationPath("resources")
    

    改为(/资源)

    @Path("/MyTestService")
    @ApplicationPath("/resources")
    

    并尝试使用正确的 maven 版本来避免 web.xml 的使用。

    【讨论】:

    • 快速修复没有帮助。我在问题中提到过,它对 GlassFish 运行时按预期工作,但对 J2EE 预览运行时不起作用。我正在使用 eclipse IDE 来执行问题中提到的步骤。
    猜你喜欢
    • 1970-01-01
    • 2015-10-18
    • 2016-05-31
    • 2021-02-24
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    相关资源
    最近更新 更多