Boldijar Paul
Boldijar Paul

Reputation: 5495

java - basic web app not working with jersey

My web.xml file

    <!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>RestServlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.pack.service</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>RestServlet</servlet-name>
    <url-pattern>/api/*</url-pattern>
  </servlet-mapping>
</web-app>

My class

  package com.pack.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/version")
public class Version {

    private static final String VERSION="1.0.0";


    @GET
    @Produces(MediaType.TEXT_HTML)
    public static String getVersion() {
        return VERSION;
    }

}

When I run the project with tomcat it takes me to http://localhost:8080/LeaderboardService/

I can see a Hello World.

If i go to http://localhost:8080/LeaderboardService/api/version I get 404. Why?

Edit 1 Removed static from the method

My class:

  package com.boldijarpaul.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/version")
public class Version {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String getVersion() {
        return "hello stackoverflow";
    }

}

But after I restart tomcat and run the project enter image description here

And when I want to call my API enter image description here

My project structure,maybe here is the problem?

enter image description here

I've also tried to manually deploy to apache, by copying the .war file inside the webapp. Same thing War file - https://www.sendspace.com/file/4cd4m0

Upvotes: 0

Views: 182

Answers (1)

RPresle
RPresle

Reputation: 2561

As it was told in the comment, your rest method must not be static. The instance of the Rest object is managed by Jersey at runtime.

Take off the static keyword from the getVersion() method.

Upvotes: 1

Related Questions