- Turn JSON support on in your servlet's web.xml:
- This example also uses compression so add the gzip filters too.
- Annotate your POJO with JAXB. Make sure you have an empty constructor. I like to make the member variables private and force use of public getters. I also elect to ignore unknown properties.
- Set up your web service. Since you're returning JSON, jQuery will be happy with basically no further consideration. Note I didn't define a setBlah() above but I hope you get the picture.
- Connect your WebResource properly. Note the addition of the JacksonJsonProvider class. The gzip filter is not required but you should probably use it.
- Now request your POJO response (note the use of gzip here):
- You can also pass POJOs to the web service using POSTs. This snippet assumes you annotated class Bar is a similar manner to Foo and the "/bar" POST endpoint accepts a Bar object.
Edited on 2/10/13 to include compression in example.