I have recently pivoted into backends and I have to create a backend for a web application. It will be with the following config:
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.
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:
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>
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'
}
@Path(value = "/hello")
public class Hello {
@GET
public String greet() {
return "Hello world!";
}
}
http://localhost:8080/<project_name>/hello
URL.