0

This might be a pretty simple question, may be it's a duplicate (though I did not find any till now), but somehow I'm stuck and need to resolve it as soon as possible.

Basically, I am trying to make a simple RESTful web service using JAX-RS API. Somehow, I am unable to so. This is my first encounter with web services and I am pretty sure I am doing a silly mistake though after lot of googling I have failed to resolve it.

So, here is my project structure -

enter image description here

MessageResource.java

package com.rest.jersey.messenger.resource;

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

@Path("/messages")
public class MessageResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getMessage() {
        return "Hello World";
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
         id="WebApp_ID" 
         version="3.0">
  <display-name>messenger</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Jersey_REST_Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.rest.jersey.messenger.resource</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey_REST_Service</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
  </servlet-mapping>
</web-app>

URL I am hitting is : http://localhost:8090/messenger/webapi/messages

Result: I am getting error page -

HTTP Status 404 - /messenger/webapi/messages

type Status report
message /messenger/webapi/messages
description The requested resource is not available.

Note: I'm not using maven. I want to set-up the project manually. And, I am using port 8090 instead of 8080.

[Edit]

I am getting below warning while starting Tomcat server:

WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Messenger' did not find a matching property.

I have tried below solution. Still same error.

Double click on your tomcat server. 
It will open the server configuration. 
Under server options check ‘Publish module contents to separate XML files’ checkbox
Kartic
  • 2,935
  • 5
  • 22
  • 43
  • This warning has nothing to do with the wrong url. I have the exact same warning and it works fine. Check here on what it means http://stackoverflow.com/questions/3566146/setting-property-source-to-org-eclipse-jst-jee-serverjsftut-did-not-find-a – Alkis Kalogeris Sep 06 '15 at 18:56

1 Answers1

2

The problem is your Context root. Try this url instead

http://localhost:8090/Messenger/webapi/messages

Basically your context root is the name of your web application. In this case it's Messenger instead of messenger. So the solution:

  1. You can just change the name of your project and be done with it.
  2. In order to change it via web.xml you have to follow vendor specific steps. Check more here
  3. If you want to change it via eclipse check this image. Check more here enter image description here
Community
  • 1
  • 1
Alkis Kalogeris
  • 17,044
  • 15
  • 59
  • 113
  • Still the same error. I am adding some more information. Hope it will be informative. – Kartic Sep 06 '15 at 18:43
  • The warning you are getting has nothing to do with it. I'm getting exactly the same warning, but it still works. Check here http://stackoverflow.com/questions/3566146/setting-property-source-to-org-eclipse-jst-jee-serverjsftut-did-not-find-a – Alkis Kalogeris Sep 06 '15 at 18:51
  • What exactly did you try? – Alkis Kalogeris Sep 06 '15 at 18:51
  • Changed project name to `messenger` and tried `http://localhost:8090/messenger/webapi/messages` – Kartic Sep 06 '15 at 19:01
  • Did you refactor or just rename? Did you refresh/rebuild/rerun? Try cleaning explicitly the project – Alkis Kalogeris Sep 06 '15 at 19:06
  • I did `refactor`. I had tried using `Maven` as well and instead of getting `Got it!` I am getting same error. Something terribly wrong here. – Kartic Sep 06 '15 at 19:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88947/discussion-between-alkis-and-kartic). – Alkis Kalogeris Sep 06 '15 at 19:19