4

These are the steps I followed to create a simple RESTful Web Service with Jax-RS in Eclipse IDE for Java EE.

  • Create a new Dynamic Web Project (Name : TestExample)
    • Select target runtime as J2EE Preview
    • Dynamic web module version : v3.1
    • Configuration type : custom with following project facets
      • Dynamic Web Module : v3.1
      • Java : v1.8
      • JAX-RS (REST Web Services) : v2.0
    • Check "Generate web.xml deployment descriptor"
  • Under Java Resources (in Project Explorer) create a new package (my.test.example) and a class under the same (TestService)
  • Import external jar file javax.ws.rs-api-2.0.jar and add it to build path to resolve javax.ws.rs import error
  • 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>
    
  • Run the project

Opening this url : http://localhost:8080/TestExample/resources/MyTestService/sayHello in a browser returns this :

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

Console Output

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

The expected output would be Hello World. What am I missing here?

The steps followed are based on a tutorial on Youtube : Java EE Tutorial #18 - RESTful Web Services with Jax-RS.

Eclipse Version : 2020-03 (4.15.0)

Note: I got it to work with GlassFish Runtime, however would still like to know why it doesn't work with J2EE Runtime.

Project Structure in Eclipse :

project structure

Saurabh P Bhandari
  • 6,014
  • 1
  • 19
  • 50

2 Answers2

0

Quick

  • Remove / from @ApplicationPath("/resources")
  • web.xml is not required

Explanation

According to this samples:

The argument for @ApplicationPath() is a simple word, without /

Try with:

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

Instead (/resources)

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

And try to use correct maven versions to avoid the web.xml usage.

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
  • The quickfix doesn't help. I have mentioned in the question, it works as expected for GlassFish Runtime but doesn't for J2EE Preview Runtime. I am using eclipse IDE to follow the steps mentioned in the question. – Saurabh P Bhandari Jun 06 '20 at 00:04
0

As seen in the project structure, J2EE Preview runtime does not include its own JAX-RS implementation. However, in GlassFish Runtime a reference implementation of JAX-RS is already included.

I did the following to get it to work with J2EE Preview runtime :

Instead of doing this step

Import external jar file javax.ws.rs-api-2.0.jar and add it to build path to resolve javax.ws.rs import error

I converted the eclipse project to a maven project and in pom.xml added the following dependency1

<!-- 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>

In web.xml2, I added this

<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>

Note

As noted here, Servlet 3.0 onwards web.xml is optional. Annotations can be used instead. However, as seen in this question Why won't the preview server in Eclipse respect my @WebServlet annotation? annotations doesnot seem to work with J2EE runtime, hence the use of web.xml.

References

1Jersey Dependencies for Servlet based server-side application

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

Community
  • 1
  • 1
Saurabh P Bhandari
  • 6,014
  • 1
  • 19
  • 50