1

I have recently pivoted into backends and I have to create a backend for a web application. It will be with the following config:

  • IDE: Eclipse
  • Build Tool: Gradle (or Maven)
  • Jakarta EE 9
  • REST Implementation: Jersey 3
  • Jakarta Servlet: 5.0
  • Server: Tomcat 10
  • Language: Java 11
  • Dynamic Web Module Version: 5.0

I tried creating with the Dynamic Web Project and Gradle Project in Eclipse and by reading the guide here but am unable to correctly get all the features properly. I would like a step-by-step guide on how to do this.

Also, I am unsure whether to use Gradle or Maven for this. I have experience with Gradle as I have made Android apps but all the tutorials for Jersey use Maven.

khateeb
  • 5,265
  • 15
  • 58
  • 114

1 Answers1

2

It doesn't really matter whether you use Maven or Gradle: both will do the job. However I would advice against using Jakarta EE 9 for now: the Eclipse plugins still have some quirks when dealing with it. E.g. you can set the Servlet API for an Eclipse project to 5.0, but Eclipse will refuse to deploy it on a server.

To startup with Jersey you just need to:

  1. Create a Dynamic Web Project (version 4.0) and create a web.xml like this:
<?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_4_0.xsd"
    id="WebApp_ID" version="4.0">
    <display-name>gradle-jersey</display-name>
    <!-- No class name, Jersey will pick it up -->
    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
    </servlet>
    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>
  1. In the project's contextual menu run Configure > Add gradle nature (I assume you have installed the Buildship plugin),
  2. Create a build.gradle file with content:
plugins {
    id 'war'
}
repositories {
    mavenCentral()
}
dependencies {
    implementation group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version: '2.34'
    implementation group: 'org.glassfish.jersey.inject', name: 'jersey-hk2', version: '2.34'
}
eclipse.wtp.facet {
    // Change the version of the Dynamic Web Module facet
    facet name: 'jst.web', version: '4.0'
    def oldJstWebFacet = facets.findAll {
        it.name == 'jst.web' && it.version == '2.4'
    }
    facets.removeAll(oldJstWebFacet)
    // Add the JAX-RS (REST Web Services) facet
    facet name: 'jst.jaxrs', version: '2.1'
}
  1. In the project's contextual menu run Gradle > Refresh Gradle Project,
  2. Eclipse should now have Gradle's dependencies in its Build path...,
  3. You can create a simple JAX-RS resource:
@Path(value = "/hello")
public class Hello {

   @GET
   public String greet() {
      return "Hello world!";
   }
}
  1. You can use "Run > Run on a server" to run the project. Your resource will be under the http://localhost:8080/<project_name>/hello URL.
Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43
  • Thanks. This worked although when I was using the `web.xml` version, I got the error `No servlet class has been specified for servlet javax.ws.rs.core.Application` but when I used the annotation version like this: https://stackoverflow.com/a/9382937/1797689, it worked. What are the advantages of one config type over the other? I also used Jersey 3.0.2 but I am seeing in the docs that 2.34 is the latest. Should we use Jersey 3 or 2? – khateeb May 08 '21 at 07:40
  • Another thing that happened when I refreshed the Gradle project was the `Deployment Descriptor` and `JAX-WS Web Services` which appear under the project dropdown in the Explorer vanished. Why did this happen and how can I get them back? – khateeb May 08 '21 at 08:06
  • 1
    My example is for Jersey 2.34 and Servlet API 4.0: as I mentioned before Eclipse has still some quirks when working with Jakarta EE 9. Jersey 3.0 is for Jakarta EE 9 (`jakarta.ws.rs`), while Jersey 2.x is for Jakarta EE 8 (`javax.ws.rs`). Also Tomcat 10 is Jakarta EE 9, while Tomcat 9 is Jakarta EE 8. – Piotr P. Karwasz May 08 '21 at 08:46
  • 1
    For the changes in the facets of your project caused by the call to _Refresh Gradle Project_, see [this question](https://stackoverflow.com/q/48828869/11748454). You need to add some configuration to your `build.gradle` file. – Piotr P. Karwasz May 08 '21 at 09:05