Despicable
Despicable

Reputation: 3957

Java Restful Web service 404 Error

I am trying to make a RESTFUL JAVA WEB SERVICE using eclipse and maven in Apache Tomcat. Everything seems fine to me and it is not showing any error but when open the URL it shows 404 Error.

I am trying to call it via this URL http://localhost:8080/first/rest
WEB.xml

<!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>first</display-name>
    <servlet>
        <servlet-name>firstapp</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.first.pkg</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>firstapp</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>


pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>task</groupId>
  <artifactId>first</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>first Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <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>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.9</version>
        </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src/main/java</sourceDirectory>
  </build>
</project>


RestServ.java

package com.first.pkg;

import java.util.Date;

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

@Path("/")
public class RestServ {

    @GET
    @Produces("text/html")
    public Response getStartingPage()
    {
        String output = "<h1>Hello World!<h1>" +
                "<p>RESTful Service is running ... <br>Ping @ " + new Date().toString() + "</p<br>";
        return Response.status(200).entity(output).build();
    }
}

Upvotes: 0

Views: 734

Answers (1)

Arthur Eirich
Arthur Eirich

Reputation: 3648

<display-name>first</display-name>

is a description part and I don't think you can call your web service via this name. Instead, try calling it like

http://localhost:8080/{nameOfYourWARFileHere}/rest

Also make sure to put rest into your @Path("/") annotation in RestServ.java right after the slash otherwise the request mapping won't work.

Upvotes: 2

Related Questions