Java 在dropwizard REST API中获取具有相同值的两个字段

Java 在dropwizard REST API中获取具有相同值的两个字段,java,json,rest,dropwizard,jersey-2.0,Java,Json,Rest,Dropwizard,Jersey 2.0,我正在使用dropwizard和STS开发一个ReSTful API。这是为了学习,所以我不是在创建db,我只是将值存储在静态数据hashmap中 我发帖添加一本带有JSON请求“request.JSON”的新书,然后使用我的“booksource.JAVA”存储这些值,并使用“BOOKREPOSITORY”保存到内存中。我得到的响应是“response.json” 自从我在我的域中添加@JsonProperty以匹配request.json之后,我得到了两个发布日期。在此之前,它运行良好 下面

我正在使用dropwizard和STS开发一个ReSTful API。这是为了学习,所以我不是在创建db,我只是将值存储在静态数据hashmap中

我发帖添加一本带有JSON请求“request.JSON”的新书,然后使用我的“booksource.JAVA”存储这些值,并使用“BOOKREPOSITORY”保存到内存中。我得到的响应是“response.json”

自从我在我的域中添加@JsonProperty以匹配request.json之后,我得到了两个发布日期。在此之前,它运行良好

下面是附加的代码,我不知道该调试什么

DOMAIN/BOOK.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;
                }
    }
BOOKRESOURCE.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();
    }
BOOKREPOSITORY.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;
    }

从类中删除
getPublicationDate()
方法,或将其添加到
@JsonIgnoreProperties

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;
    }
{
    "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"
    }]
}