使用resteasy建立restful服务,在客户端调用时,报错
Caused by: java.lang.RuntimeException: A GET request cannot have a body.
http://localhost:8080/RestTest/rest/hello/books/2
{"isbn":"2","title":"The Stoker"}
部署到jboss7的服务端代码
@Local @Path("/hello") @Consumes(MediaType.APPLICATION_JSON) public interface IHelloWorldLocal { @GET @Path("/books") @Produces({ MediaType.APPLICATION_JSON }) public List<Book> getAllBooks(); @GET @Path("/books/{id}") @Produces({ MediaType.APPLICATION_JSON }) public Book findBook(@PathParam("id") String id);
public class HelloWorldBean implements IHelloWorldLocal { static { Book[] bookarr = new Book[] { new Book("1", "The Judgment"), new Book("2", "The Stoker"), new Book("3", "Jackals and Arabs"), new Book("4", "The Refusal") }; for (Book book : bookarr) { books.put(book.getIsbn(), book); } } public List<Book> getAllBooks() { ...... } public Book findBook(String id) { ...... }
客户端测试代码
public static void main(String arg[]) { // TODO Auto-generated method stub try { // This initialization only needs to be done once per VM RegisterBuiltin.register(ResteasyProviderFactory.getInstance()); String REQUEST_PATH = "http://localhost:8080/RestTest/rest/"; IHelloWorldLocal client = ProxyFactory.create(IHelloWorldLocal.class, REQUEST_PATH); System.out.println(client.findBook("2")); List<Book> books = client.getAllBooks();
把接口类IHelloWorldLocal方法中的@GET改为@POST即可