我正在使用 dropwizard 和 STS 开发一个 ReSTful API。这是出于学习目的,所以我没有创建数据库,我只是将值存储在静态数据哈希图中。
我发布以使用 JSON 请求“request.json”添加新书,然后使用“BOOKRESOURCE.JAVA”存储值并使用“BOOKREPOSITORY”保存到内存中。我得到的回复是“response.json”
自从我在我的域中添加 @JsonProperty 以匹配 request.json 后,我得到了两个发布日期。在此之前它工作正常。
下面附上代码,我不知道要调试什么。
域/书籍.JAVA
package edu.sjsu.cmpe.library.domain;
import java.util.List;
import org.hibernate.validator.constraints.NotEmpty;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Book {
private long isbn;
@NotEmpty
private String title;
@NotEmpty
@JsonProperty("publication-date")
private String pubdate;
private String language;
@JsonProperty("num-pages")
@NotEmpty
private int numPages;
private String status = "available";
@JsonProperty("authors")
private List<Author> author;
/*add more fields here
ISBN (Key) # You will generate this key.
Title (Required field)
Publication Date (Required field)
Language (Optional field)
Number of Pages (Optional field)
Status {available, checked-out, in-queue, or lost} # default to ‘available’
*/
/**
* @return the isbn
*/
public long getIsbn() {
return isbn;
}
/**
* @param isbn
* the isbn to set
*/
public void setIsbn(long isbn) {
this.isbn = isbn;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title
* the title to set
*/
public void setTitle(String title) {
this.title = title;
}
public String getPublicationDate() {
return pubdate;
}
public void setPublicationDate(String date) {
this.pubdate = date;
}
public String getLanguage() {
return language;
}
/**
* @param title
* the title to set
*/
public void setLanguage(String language) {
this.language = language;
}
public int getNumPages() {
return numPages;
}
/**
* @param isbn
* the isbn to set
*/
public void setNumPages(int numPages) {
this.numPages = numPages;
}
public String getStatus() {
return status;
}
/**
* @param title
* the title to set
*/
public void setStatus(String status) {
this.status = status;
}
public List<Author> getAuthor() {
return author;
}
public void setAuthor(List<Author> author) {
this.author = author;
}
}
请求.json
{
"title" : "Programming Amazon EC2",
"publication-date" : "2/11/2011",
"language" : "eng",
"num-pages": 185,
"status" : "available",
"authors" : [
{ "name" : "Jurg Vliet" },
{"name" : "FlaviaPagenelli"} ]
}
图书资源.JAVA
@Path("/v1/books")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class BookResource {
/** bookRepository instance */
private final BookRepositoryInterface bookRepository;
// private final ReviewRepositoryInterface reviewrepository;
/**
* BookResource constructor
*
* @param bookRepository
* a BookRepository instance
*/
public BookResource(BookRepositoryInterface bookRepository) {
this.bookRepository = bookRepository;
}
@GET
@Path("/{isbn}")
@Timed(name = "view-book")
public BookDto getBookByIsbn(@Valid @PathParam("isbn") LongParam isbn) {
Book book = bookRepository.getBookByISBN(isbn.get());
BookDto bookResponse = new BookDto(book);
String location = "/books/" + book.getIsbn();
bookResponse.addLink(new LinkDto("view-book", location, "GET"));
bookResponse.addLink(new LinkDto("update-book", location, "PUT"));
bookResponse.addLink(new LinkDto("delete-book", location, "DELETE"));
bookResponse.addLink(new LinkDto("create-review", location + "/reviews", "POST"));
//if (reviewrepository.getAllReviews((Long)isbn.get()).size()==0){
bookResponse.addLink(new LinkDto("get-all-reviews", location + "/reviews", "GET"));
//}// Add other links if needed/
return bookResponse;
}
@POST
@Timed(name = "create-book")
public Response createBook(Book request) {
// Store the new book in the bookRepository so that we can retrieve it.
Book savedBook = bookRepository.saveBook(request);
String location = "/books/" + savedBook.getIsbn();
BookDto bookResponse = new BookDto(savedBook);
bookResponse.addLink(new LinkDto("view-book", location, "GET"));
bookResponse.addLink(new LinkDto("update-book", location, "PUT"));
bookResponse.addLink(new LinkDto("delete-book", location, "DELETE"));
bookResponse.addLink(new LinkDto("create-review", location + "/reviews", "POST"));
// Add other links if needed/
return Response.status(201).entity(bookResponse).build();
}
图书存储库.JAVA
public class BookRepository implements BookRepositoryInterface {
/** In-memory map to store books. (Key, Value) -> (ISBN, Book) */
private final ConcurrentHashMap<Long, Book> bookInMemoryMap;
private static long authID;
/** Never access this key directly; instead use generateISBNKey() */
private long isbnKey;
public BookRepository(ConcurrentHashMap<Long, Book> bookMap) {
checkNotNull(bookMap, "bookMap must not be null for BookRepository");
bookInMemoryMap = bookMap;
isbnKey = 0;
}
/**
* This should be called if and only if you are adding new books to the
* repository.
*
* @return a new incremental ISBN number
*/
private final Long generateISBNKey() {
// increment existing isbnKey and return the new value
return Long.valueOf(++isbnKey);
}
/**
* This will auto-generate unique ISBN for new books.
*/
@Override
public Book saveBook(Book newBook) {
checkNotNull(newBook, "newBook instance must not be null");
// Generate new ISBN
Long isbn = generateISBNKey();
newBook.setTitle(newBook.getTitle());
newBook.setIsbn(isbn);
newBook.setLanguage(newBook.getLanguage());
newBook.setNumPages(newBook.getNumPages());
newBook.setPublicationDate(newBook.getPublicationDate());
newBook.setStatus(newBook.getStatus());
for (int i=0; i< newBook.getAuthor().size(); i++)
{
newBook.getAuthor().get(i).setId(++authID);
}
newBook.setAuthor(newBook.getAuthor());
// TODO: create and associate other fields such as author
// Finally, save the new book into the map
bookInMemoryMap.putIfAbsent(isbn, newBook);
return newBook;
}
响应.JSON
{
"book": {
"isbn": 4,
"title": "Programming Amazon EC2",
"language": "eng",
"status": "available",
"publicationDate": "2/11/2011",//this is the extra field that is not required.
"publication-date": "2/11/2011",
"num-pages": 185,
"authors": [{
"id": 7,
"name": "Jurg Vliet"
}, {
"id": 8,
"name": "FlaviaPagenelli"
}]
},
"links": [{
"rel": "view-book",
"href": "/books/4",
"method": "GET"
}, {
"rel": "update-book",
"href": "/books/4",
"method": "PUT"
}, {
"rel": "delete-book",
"href": "/books/4",
"method": "DELETE"
}, {
"rel": "create-review",
"href": "/books/4/reviews",
"method": "POST"
}]
}