Learner
Learner

Reputation: 21435

Getting 404 error in Jersey with no web.xml

I am following Jersey tutorial to develop simple Jersey web application.

By following Section - Example 2.9. Deployment of a JAX-RS application using @ApplicationPath with Servlet 3.0

I have created created below program:

@ApplicationPath("resources")
public class MyApplication extends PackagesResourceConfig {
    public MyApplication() {
        super("com.examples");
    }
}

and I have below basic Resource class:

@Path("/helloworld")
public class HelloWorldResource {

    @GET
    @Produces("text/plain")
    public String getClichedMessage() {
        return "Hello World";
    }
 }

I am using Jersey-1.19 version, I am not having any web.xml file in my web application. Now I am deploying my application on Tomcat 7 server.

When I try to access the URL as : http://localhost:8080/myapp/resources/helloworld I am getting error as

HTTP Status 404 - /myapp/resources/helloworld type Status report

message: /myapp/resources/helloworld

description: The requested resource is not available.

Upvotes: 1

Views: 1022

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209082

You need the jersey-servlet dependency

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.19</version>
</dependency>

if you want to go with no web.xml. It has the JerseyServletContainerInitializer required to load the application.


And just for any future readers that come across this looking for a Jersey 2.x solution, you need the following dependency to work with no web.xml

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>${jersey2.version}</version>
</dependency>

for the same reason - it has the JerseyServletContainerInitializer

Upvotes: 1

Related Questions