I’m encountering an issue with my JAX-RS application where a resource class annotated with @Path is not automatically collected by the server, resulting in a 404 error when attempting to access the endpoint.
Project Structure:
com.example │ Main.java │ └───hello Hello.java
Main.java:
package com.example;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import java.io.IOException;
import java.net.URI;
public class Main {
public static final String BASE_URI = "http://localhost:8080/";
public static HttpServer startServer() {
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI));
}
public static void main(String[] args) throws IOException {
final HttpServer server = startServer();
System.out.println(String.format("Jersey app started with endpoints available at %s%nHit Ctrl-C to stop it...", BASE_URI));
System.in.read();
server.stop();
}
}
Hello.java:
package com.example.hello;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("hello")
public class Hello {
@GET
public String sayHello() {
return "Hello World";
}
}
Whenever I explicitly register the package using ResourceConfig constructor like this:
final ResourceConfig rc = new ResourceConfig().packages("com.example.hello");
it works, but otherwise, it doesn’t.
I’ve read this Stack Overflow answer which suggests that using the default application class without registering anything should automatically include all resources, but that doesn’t seem to be happening in my case.
What could be causing this issue? How can I ensure that my resource class is properly collected and accessible through the defined URI path without explicit registration?