0

我想建立一个关于如何使用 Restygwt 的简单演示项目,用于学习。我遵循了这个教程:https ://ronanquillevere.github.io/2014/03/16/gwt-rest-app.html 到目前为止,我在我的客户端上得到了这个:在我的 SearchPresenter 方法 searchBook(String isbn) 被触发时我点击一个按钮

    @Override
public void searchBook(String isbn) {
    clientContext.getBookStoreClient().getBook(isbn, new MethodCallback<List<Book>>() {
        @Override
        public void onFailure(Method method, Throwable exception) {
            Window.alert("Failure");
            GWT.log(exception.getMessage());
            GWT.log(stackTraceToString(exception));


        }
        @Override
        public void onSuccess(Method method, List<Book> response) {
            for (Book book : response) {
                Window.alert(book.getIsbn() + " " + book.getAuthor());
            }
        }

    });
}

private String stackTraceToString(Throwable e) {
    StringBuilder sb = new StringBuilder();
    for (StackTraceElement element : e.getStackTrace()) {
        sb.append(element.toString());
        sb.append("\n");
    }
    return sb.toString();
}

我的 BookstoreClient 看起来像这样:

@Path("/api/bookstore/books")
public interface BookstoreClient extends RestService{

@GET
public void getBooks(MethodCallback<List<Book>> callback);

@GET
@Path("/{isbn}")
public void getBook(@PathParam("isbn") String isbn, MethodCallback<List<Book>> callback);
}

我服务器上的 BookResource:

@Path("books")
public class BookResource {

Map<String, Book> books;

public BookResource() {
    books = new HashMap<>();

    Book book1 = new Book("1", "Max Mustermann");
    books.put(book1.getIsbn(), book1);

    Book book2 = new Book("2", "Erika mustermann");
    books.put(book2.getIsbn(), book2);
}

@GET
@Produces("application/json")
public Collection<Book> getBooks() {
    return books.values();
}

@GET
@Path("/{isbn}")
@Produces("application/json")
public Book getBook(@PathParam("isbn") String isbn) {
    return books.get(isbn);
}
}

还有我共享包中的书

public class Book {

private final String isbn;
private final String author;

@JsonCreator
public Book(@JsonProperty("isbn")String isbn, @JsonProperty("author")String author) {
    this.isbn = isbn;
    this.author = author;
}

public String getIsbn() {
    return isbn;
}

public String getAuthor() {
    return author;
}

}

但是,如果我单击 GUI 上的按钮,我会跳转到 onFailure 并获得异常:

响应不是有效的 JSON 文档

Unknown.Throwable_4_g$(GWT_Selenium-0.js@8:4312)
Unknown.Exception_4_g$(GWT_Selenium-0.js@18:4550)
Unknown.RuntimeException_4_g$(GWT_Selenium-0.js@18:4597)
Unknown.new ResponseFormatException_2_g$(GWT_Selenium-0.js@25:51000)
Unknown.parseResult_2_g$(GWT_Selenium-0.js@23:33367)
Unknown.parseResult_1_g$(GWT_Selenium-0.js@15:33355)
Unknown.onResponseReceived_0_g$(GWT_Selenium-0.js@27:33312)
Unknown.fireOnResponseReceived_0_g$(GWT_Selenium-0.js@17:14339)
Unknown.onReadyStateChange_0_g$(GWT_Selenium-0.js@28:14647)
Unknown.<anonymous>(GWT_Selenium-0.js@18:30998)
Unknown.apply_0_g$(GWT_Selenium-0.js@28:5498)
Unknown.entry0_0_g$(GWT_Selenium-0.js@16:5554)
Unknown.<anonymous>(GWT_Selenium-0.js@14:5534)

但如果我去网址:

http://127.0.0.1:8888/api/bookstore/books

我的浏览器中显示以下内容

[{"isbn":"1","author":"Max Mustermann"},{"isbn":"2","author":"Erika mustermann"}]

我是否错过了什么或搞砸了 RestService 的实施?提前感谢您的任何评论,您的帮助很重要

4

1 回答 1

2

您的问题是当您单击按钮时会发生错误,并且该按钮通过 isbn 进行搜索 - 它GET /api/bookstore/books不像您在浏览器中测试的那样调用。这就是它的实际作用:

@GET
@Path("/{isbn}")
public void getBook(@PathParam("isbn") String isbn, MethodCallback<List<Book>> callback);

此客户端方法需要List<Book>, 但在您定义的服务器上

@GET
@Path("/{isbn}")
@Produces("application/json")
public Book getBook(@PathParam("isbn") String isbn) 

它返回一本书,而不是包含在列表中。这可能是您的错误的根源。

于 2017-11-24T02:04:52.370 回答