Subho
Subho

Reputation: 923

How to call rest with jax rs

I am new to jax rs web service. I was studying from this link- http://www.vogella.com/tutorials/REST/article.html

Now when I was trying to do my first rest service i faced some errors. This is my service code

package de.vogella.jersey.first;

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

@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";
  }
 } 

My web.xml is

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd" 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>de.vogella.jersey.first</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey REST Service</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>de.vogella.jersey.first</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>
</web-app>

Now when I was running this code I faced HTTP Status 404 error. Please any one help me. I give all the jars from http://jersey.java.net/ Please help me.

Upvotes: 0

Views: 818

Answers (1)

HJK
HJK

Reputation: 1382

If you are using jersey 2.x you Web.xml servlet should be as following

    <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>de.vogella.jersey.first</param-value>
      </init-param>
    </servlet>

In jersey 2.x you need to refer "org.glassfish" and your resource URL would be localhost:your_port/your_app_name/rest/hello

Upvotes: 1

Related Questions