2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Rest Web services returning a 404?

In Eclipse I create simple RestWeb Service appliaction.I followed vogella tutorial.I took maven web application.

In Pom I added 1.17.1 version jersery related all the dependecies.

web.xml as

==========

<servlet>
     <servlet-name>Jersey REST Service</servlet-name>
     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <!-- Register resources and providers under com.ws.first package. -->
      <init-param>
         <param-name>jersey.config.server.provider.packages</param-name>
         <param-value>com.ws</param-value>
      </init-param>
      <init-param>
            <param-name>com.sun.jersey.config.feature.Trace</param-name>
            <param-value>true</param-value>
        </init-param>
    <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>

Java class is: Hello.java

@Path("/hello")
public class Hello {

  // This method is called if TEXT_PLAIN is request
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey";
  }

  // This method is called if XML is request
  @GET
  @Produces(MediaType.TEXT_XML)
  public String sayXMLHello() {
    return

"<?xml version="1.0"?>" + "<hello> Hello Jersey" + "</hello>";
  }

  // This method is called if HTML is request
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
  }

}

Try the url as:localhost:8090/FirstWS/rest/hello

but it gives 404 error:: The requested resource is not available.












userimage

The things you trying to do to get the benefit of restful webservice i will not suggest to for that way:

The way i will prefer will be as below :

Maven pom.xml configuration:

Add repository:

    <repositories>
        <repository>
            <id>maven2-repository.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
            <layout>default</layout>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.9</version>
        </dependency>
    </dependencies>


In web.xml add below method:

    <servlet>
        <servlet-name>jersey-helloworld-serlvet</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.kodetalk.restful</param-value> <!-- The package where all the class file will present-->
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>jersey-helloworld-serlvet</servlet-name>
        <url-pattern>/kodetalk/*</url-pattern>
    </servlet-mapping>

    
Now i need to write the class file to access:

package com.kodetalk.restful;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

@Path("/kodetalk")
public class HelloWorldREST {

    @GET
    @Path("/{parameter}")
    public Response responseMsg( @PathParam("parameter") String parameter) {
        String output = "Parameter I passed : " + parameter + " : ";
        return Response.status(200).entity(output).build();
    }
    
    @GET
    @Path("/restHello")
    public String responseMsg1() {
        return "Hellow world...";
    }
}


The url will be : http://localhost:<port number>/kodetalk/kodetalk/resrHello // To display Hellow World...

and http://localhost:<port number>/kodetalk/kodetalk/send Parameter // Will print : Parameter I passed : send Parameter


The process you trying to do will cause lots of trouble so try as mentioned.

userimage

I got the answer. Previously I placed "web.xml" file in following folder structure

RestWebServiesProject --> src --> main --> webapp -->WEB-INF --> web.xml


But Now I changed it to WebContent Folder,so folder structure :

RestWebServiesProject --> WebContent --> WEB-INF --> web.xml



At time of creation "maven Project",it not create the WebContent folder by default,so I manually added the WebContent folder in my project.

userimage
Thanks Moe .I tried same thing in my system but it again gives same error
Answer is