问题
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
回答1:
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.
回答2:
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
回答3:
To fix your app:
- Use
<web-app version="3.0" ..
- Add servlet mapping as in @austin's answer
- Optionally, read section
2.3.2 Servlet
of jax-rs-1.1 specification, which will help you setup the rest ofweb.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.
来源:https://stackoverflow.com/questions/11405158/jboss-as-7-restful-webservices-not-auto-deploying