Ich wollte ein einfaches Demo-Projekt zur Verwendung von Restygwt einrichten, um zu lernen. Ich folgte diesem Tutorial: https://ronanquillevere.github.io/2014/03/16/gwt-rest-app.html Bisher habe ich diese auf meinem Client: In meinem SearchPresenter die Methode Erweiterte (String isbn) ausgelöst wird, wenn ich auf eine Schaltfläche klickenGWT - Restygwt - Antwort war kein gültiges JSON-Dokument
@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();
}
Meine BookstoreClient wie folgt aussieht:
@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);
}
die BookResource auf meinem Server:
@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);
}
}
Und der Boo k in meinem gemeinsamen Paket
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;
}
}
Aber wenn ich den Knopf auf meinem GUI klicken springe ich in die onFailure und erhalten die Ausnahme:
Antwort war kein gültiges JSON Dokument
Unknown.Throwable_4_g$([email protected]:4312)
Unknown.Exception_4_g$([email protected]:4550)
Unknown.RuntimeException_4_g$([email protected]:4597)
Unknown.new ResponseFormatException_2_g$([email protected]:51000)
Unknown.parseResult_2_g$([email protected]:33367)
Unknown.parseResult_1_g$([email protected]:33355)
Unknown.onResponseReceived_0_g$([email protected]:33312)
Unknown.fireOnResponseReceived_0_g$([email protected]:14339)
Unknown.onReadyStateChange_0_g$([email protected]:14647)
Unknown.<anonymous>([email protected]:30998)
Unknown.apply_0_g$([email protected]:5498)
Unknown.entry0_0_g$([email protected]:5554)
Unknown.<anonymous>([email protected]:5534)
Aber wenn ich zu uRL:
http://127.0.0.1:8888/api/bookstore/books
ich folgendes erhalten sie in meinem Browser
gezeigt[{"isbn":"1","author":"Max Mustermann"},{"isbn":"2","author":"Erika mustermann"}]
Habe ich etwas verpasst oder über die Umsetzung des RestService vermasselt? Vielen Dank im Voraus für jeden Kommentar, youre Hilfe ist apreciatet
Danke, das hat mein Problem gelöst – KilledByCheese