0

Im very new to Jersey REST and I'm trying to build a sample library system. I have created my BookResource.java and I want it to display the following output in browser:

{
 “book” : {
  “isbn” : 1,
  “title” : “Programming Amazon EC2”,
  “publication-date” : “2/11/2011”,
  “language” : “eng”,
  “num-pages”: 185,
  “status” : “available”,
  “authors” : [
      { “rel”: “view-author”, “href”: “/books/1/authors/1”, “method”: “GET” },
      { “rel”: “view-author”, “href”: “/books/1/authors/2”, “method”: “GET” }
  ]
  },
 "links" : []
}

My Book.java is below:

public class Book 
{
    private long isbn;
    private String title;

    @JsonProperty("publication-date")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM/dd/yyyy")
    private String date;

    private String language;
    private String status;

    @JsonProperty("num-pages")
    private Integer num_pages;

    @JsonProperty("authors")
    private ArrayList<Author> authorList;

    public Book() {}
    //getters and setters
}

Author.java

public class Author
{
    @JsonProperty("name")
    private String name;

    public Author() {}

    public String getAuthor() {
        return name;
    }

    public void setAuthor(String newAuthor){
        this.name = newAuthor;
    }
}

BookRepository.java

public class BookRepository implements BookRepositoryInterface {
public ArrayList<Author> getAuthorByIsbn(Long isbn)
    {
        Book newBook = bookInMemoryMap.get(isbn);
        ArrayList<Author> authorList = newBook.getAuthor();
        return authorList;
    }
}

However my actual GET response on browser is showing as:

{
    "book": {
        "isbn": 1,
        "title": "Programming EC2r",
        "language": "eng",
        "status": "available",
        "author": [{
            "name": "auth1",
            "author": "auth1"
        }, {
            "name": "auth2",
            "author": "auth2"
        }],
        "num_Pages": 185,
        "publication-date": "2/11/2011",
        "num-pages": 185,
        "authors": [{
            "name": "auth1",
            "author": "auth1"
        }, {
            "name": "auth2",
            "author": "auth2"
        }]
    }

BookResource.java

public class BookResource 
{
    @GET
    @Path("/{isbn}")
    @Timed(name = "view-book")
    public BookDto getBookByIsbn(@PathParam("isbn") LongParam isbn) 
    {
      Book book = bookRepository.getBookByISBN(isbn.get());
      BookDto bookResponse = new BookDto(book); 
      // add more links
      bookResponse.addLink(new LinkDto("view-book", "/books/" + book.getIsbn(),"GET"));     
      return bookResponse;
    }
}

I am getting multiple values for author and num_pages and cannot seem to get the output displayed correctly. I have tried @JsonPropertyOrder, but it didn't work. Also, in debug I checked that the Book resource is getting stored and retrieved correctly. It's only the JSON output which is displaying multiple values. How can I correct that? Also, how can I display the links for "authors" as required ?

Rashi
  • 41
  • 7
  • your classes seem correct. may you show your `BookResource` class ? Some things I noticed: there is no `author` field in your `Book` class, but you get `author` in your json and also you dont have `author` field in your `Author` class, but you still get such field in the json, so have you posted the correct/all classes here ? – Svetlin Zarev Mar 08 '14 at 19:08
  • The classes you show are not producing that JSON. – Brian Roach Mar 08 '14 at 19:20
  • a possible duplicate of this: http://stackoverflow.com/questions/13262662/duplicate-json-property-when-converting-java-object-to-json-string-using-jackson –  Mar 08 '14 at 19:21
  • Hi, these are the actual classes that I am using. Also the value "num_pages" also gets displayed twice. I am very confused where is JSON getting all these multiple values from, since I saw in debug that the Book object is correctly getting stored and retrieved. – Rashi Mar 08 '14 at 19:39
  • Hi! I remembered that JAckson uses the getters and the setters to access the class fields, it's not accessing them directly. But you have `@JsonProperty` on your fields and this might be causing the issue. Move all your `@JsonProperty` annotations on the **getters*** and see if this fixes it :) – Svetlin Zarev Mar 08 '14 at 19:44
  • Hi @SvetlinZarev, that sure helped ! :) So I was able to remove the duplicate "num-pages" being displayed in json output. Also I realised that I am doing something wrong while retrieving Author name (ie string) and putting it in an ArrayList in the function call: ArrayList authorList = newBook.getAuthor(); I'm trying to fix that issue now ! Thanks again :) – Rashi Mar 08 '14 at 19:53

1 Answers1

0

Thanks to @SvetlinZarev, I was able to solve the issue of multiple values being displayed by putting the @JsonProperty only on the getter methods.

@JsonProperty("name")
public String getAuthor() {
    return name;
}

It worked perfectly fine and it's displaying my correct output as below:

{
    "book": {
        "isbn": 1,
        "title": "Programming EC2r",
        "publication-date": "2/11/2011",
        "language": "eng",
        "num-pages": 185,
        "status": "available",
        "authors": [{
            "name": "auth1"
        }, {
            "name": "auth2"
        }]
    },
    "links": []
}

Thanks again to everyone for your prompt responses to a newbie like me! :)

Rashi
  • 41
  • 7