2

I'm trying to use the built in Restful WebServices with JBoss AS 7. My web.xml is..

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
       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_2_5.xsd"
       version="2.5">

</web-app>

My application class is...

package com.robert;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;

@ApplicationPath("/services")

public class HelloWorld extends Application {
private Set<Object> singletons = new HashSet<Object>();

public HelloWorld() {
    singletons.add(new Library());
}

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> classes = new HashSet<Class<?>>();
    classes.add(Library.class);
    return classes;    //To change body of overridden methods use File | Settings | File Templates.
}

@Override
public Set<Object> getSingletons() {
    return singletons;
}

}

and my class is

import javax.ws.rs.*;

@Path("/library")
public class Library {

@GET
@Path("/books")
public String getBooks() {
    return "this is all your books";
}

@GET
@Path("/book/{isbn}")
public String getBook(@PathParam("isbn") String id) {
    // search my database and get a string representation and return it
    return "Its a good book; I read it";
}

@PUT
@Path("/book/{isbn}")
public void addBook(@PathParam("isbn") String id, @QueryParam("name") String name) {
    System.out.println("Adding book "+name);
}

@DELETE
@Path("/book/{id}")
public void removeBook(@PathParam("id") String id ){
    System.out.println("Removing book "+id);

}

}

However, when I start JBoss AS7 the WebService is never started. I don't see it int he JBoss Management page and I don't see it at

http://foobar:8080/MyWar/services/library/books
user959690
  • 602
  • 9
  • 16

3 Answers3

2

Ok, I discovered the problem. Following the directions from RestEasy I had installed the latest version of RestEasy into the JBoss module. When I reverted back to the default installation it work. Note that the web.xml MUST NOT contain any reference to the Restful servlets as the JBoss deployer auto deploys RestEasy when it sees the @ApplicationPath annotation on a class. Web.xml should be empty.

user959690
  • 602
  • 9
  • 16
1

You need to add REST servlet mapping

in web.xml you need to add a servlet mapping to REST Servlet, something like this

<servlet-mapping>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

where the URL pattern should match whatever is supposed to be handled as RESTFUL (or use /* - this will make your Rest servlet handle all requests to this application)

i dont know why but i have never seen any of my Restful web services in the Web Service Section on the JBoss management console but i see my WSDL SOAP based web services in that list.

However i do see the Restful projects in the manage deployments section of the Management console

austin
  • 1,171
  • 1
  • 13
  • 32
  • But the entire point of the JBoss deployers is that it reflects across the classes in your WAR looking for the @Application annotation and adds the buildin RestEasy libraries. – user959690 Jul 10 '12 at 21:57
  • Hmmm, I get an error because javax.ws.rs.core.Application is not a Servlet... ` exception: java.lang.ClassCastException: javax.ws.rs.core.Application cannot be cast to javax.servlet.Servlet` – user959690 Jul 10 '12 at 22:02
0

To fix your app:

  1. Use <web-app version="3.0" ..
  2. Add servlet mapping as in @austin's answer
  3. Optionally, read section 2.3.2 Servlet of jax-rs-1.1 specification, which will help you setup the rest of web.xml, though it's redundant in your example.

Also you may use helloworld-rs quickstart accompanying jbossas-7 as a starting point to JavaEE 6 RESTful webapp.

Tair
  • 3,779
  • 2
  • 20
  • 33