Markus
Markus

Reputation: 1485

Implement a RESTful Web service with jersey

Hi folks i implemented a tutorial for RESTful Web services with jersey.

My Project Setup is as follows:

Folder structure:

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
  <web-app
    xmlns="http://java.sun.com/xml/ns/javaee" version="3.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
    <servlet>
       <servlet-name>RestfulContainer</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.mcnz.ws</param-value>
       </init-param>
           <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
       <servlet-name>RestfulContainer</servlet-name>
       <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>
</web-app>

HelloWorldResource.java

package com.mcnz.ws;
import javax.ws.rs.*;
@Path("helloworld")
public class HelloWorldResource {
    @GET
    @Produces("text/plain")
    public String getMessage() {
        return "Rest Never Sleeps";
    }
}

I builded the war and all seems to run but after deployment to a tomcat 7 the url

http://localhost:8080/restful/resources/helloworld 

does not respond

here my .war

Upvotes: 0

Views: 1761

Answers (3)

Pradip Bhatt
Pradip Bhatt

Reputation: 658

Recently I successfully implementing Jersey RESTful WEb service in Liferay..

You can find it...

Jersey Implementation

Upvotes: 0

mvlupan
mvlupan

Reputation: 3636

I think that your project structure is a mess.Why are your REST resources in the WEB-INF folder? You should have placed them in src/main/java. I recommend following this tutorial for your first REST project. Also looking again at your folder structure i would recommend using Maven for dependency management.You can find some tutorials here.

Upvotes: 1

sahmed24
sahmed24

Reputation: 358

You have the path a the class level, try putting it at the method level (after the @GET statement)
You can also leave the first one and another one after the @GET for example

This is one sample of mine

@Path("/user/")
public class UserResource {

    // Method for Registering a User, receives and replies a JSON
    @POST
    @Path("/register")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public StatusResult register(UserRegistrationRequest urr) {
        //Calls the controller to register the producer, and returns result
        StatusResult result = UserController.register(urr);
        return result;
    }

You would call it like this localhost:8080/restful/resources/user/register in this case

Upvotes: 0

Related Questions