Simplest Possible REST Endpoint

The following GET-request:


http://localhost:8080/hello-rest/resources/message

invokes the method whatever:

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("message")
public class MessageResource {
    @GET
    public String whatever() {
        return "hey, duke!";
    }
}

from a WAR with th name hello-rest and configured application path:

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("resources")
public class JAXRSConfiguration extends Application {}

You only need a single dependency to compile the sample:

<dependency>
	<groupId>javax</groupId>
	<artifactId>javaee-api</artifactId>
	<version>7.0</version>
	<scope>provided</scope>
</dependency>

  1. No further configuration is required
  2. No XML is involved (web.xml is not needed)
  3. The size of the hello-rest.war is 3.2 kB
  4. Deployment takes milliseconds
Optional speedup: Java EE 7 essential archetype would create an empty maven project for you.

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.

Comments:

Hi Adam,

I am trying to develop a simple rest application using Java-EE 7 api with tomcat server and when I am executing the application, if I invoke the REST Url, it is throwing 404 error. I am not sure about whether will it work with Tomcat. I tried googling before asking here. Most of the examples are given using jersey or RESTeasy. Can you please help me to resolve the issue?.

Regards
Raghav

Posted by Raghav on July 05, 2018 at 04:06 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License