users@jersey.java.net

[Jersey] Implementing a Query by Example

From: Antonio Goncalves <antonio.mailing_at_gmail.com>
Date: Tue, 22 Oct 2013 00:23:14 +0200

Hi all,

I need to implement a query by example service and be able to consume it
with the new client API.

The idea is that I have a Book entity with a JAXB annotation :

@Entity
@XmlRootElement
public class Book implements Serializable {

    @Id @GeneratedValue
    private Long id = null;

    private String isbn;
    private String title;
    private String description;
}


I now need a service that would be able to receive an 'example' of the Book
in XML format. So if the service receives :

<book><isbn>1234</isbn></book>

It will return all the books with an ISBN like '1234. If the service
receives :

<book><isbn>1234</isbn><title>Java</title><description>Best
book</description></book>

Il will return all the books with an ISBN like '1234' AND a title like
'Java' AND a description like 'Best Book'. As you can see, the query string
depends on which attributes of the entity are set. So I was thinking of
having a URI that would look like :

/rest/books/*query*=<book><isbn>1234</isbn></book>
/rest/books/*query*=<book><isbn>1234</isbn><title>Java</title><description>Best
book</description></book>

So I assume that the resource would then look like :

    @GET
    public Response findByQBE(@QueryParam("*query*") Book example) {
       ...
    }

And the client API

    Book *example* = new Book();
    example.setTitle("Java");
client.target(URL + "/books").path("/qbe").queryParam("*query*", *example*
).request(MediaType.APPLICATION_XML).get();

But that doesn't work (I get a 404).

I feel I don't need to write a provider to marshall the POJO into an XML
String. So I think I'm doing something wrong here.

Any idea

Thanks

-- 
Antonio Goncalves
Software architect and Java Champion
Web site <http://www.antoniogoncalves.org/> |
Twitter<http://twitter.com/agoncal>
 | LinkedIn <http://www.linkedin.com/in/agoncal> | Paris
JUG<http://www.parisjug.org/>
 | Devoxx France <http://www.devoxx.fr/>