调用resteasy服务报错:A GET request cannot have a body

使用resteasy建立restful服务,在客户端调用时,报错

Caused by: java.lang.RuntimeException: A GET request cannot have a body.

但是通过url访问正常

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) {
		......
	}

打包为RestTest.war发布到jboss7。

客户端测试代码

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();

可能是resteasy的一个bug, https://issues.jboss.org/browse/RESTEASY-292

把接口类IHelloWorldLocal方法中的@GET改为@POST即可


你可能感兴趣的:(调用resteasy服务报错:A GET request cannot have a body)