GWT - Restygwt - 响应不是有效的JSON文档

时间:2017-11-23 23:40:06

标签: java json gwt resty-gwt javax.ws.rs

我想建立一个关于如何使用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上的Button,我会跳转到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的实施? 在此先感谢您的任何评论,您的帮助是apreciatet

1 个答案:

答案 0 :(得分: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) 

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