23

I need to build the server side of a JSON-REST API, and I've been playing with Jersey to do it (using it's JSON-POJO mapping feature).

The problem is that even testing the simplest possible use-case has required several questions here on SO, and quite a bit of hunting around. In short, it's not a very fluid API in the style of, say, JSoup, it appears to be a tool that comes from the era where everything was XML, and then it was retrofitted for JSON.

You can see this, for example, in the requirement that POJO objects need to be annotated with @XmlRootElement even though nothing I am doing involves XML.

I'm wondering if there are other libraries, perhaps more recent, that I should consider for this that might be easier to use than Jersey?

sanity
  • 35,347
  • 40
  • 135
  • 226
  • 10
    YES! YES! FOR THE LOVE OF GOD YES! I have been asking this same type of question now over and over again and I keep getting little to nothing back. I even gave up a 300pt bounty and still nothing: [here](http://stackoverflow.com/questions/6986365/java-rest-client-jax-rs-automatic-type-mapping). [And this one was fun.](http://stackoverflow.com/questions/6312030/cxf-no-message-body-writer-found-for-class-automatically-mapping-non-simple-r). I don't know why we haven't got web services in java tied down yet. – javamonkey79 Mar 11 '12 at 09:17

7 Answers7

10

Jersey can serialize POJOs to JSON without any annotations using Jackson. You configure this by setting the JSONConfiguration.FEATURE_POJO_MAPPING property to true.

In web.xml, add the following servlet init parameter:

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

See the Jersey documentation

hakonlo
  • 103
  • 1
  • 5
  • Note that the Jersey2 documentation wrt using Jackson is imo poor/lacking/dated. Have a look at this question and answer http://stackoverflow.com/questions/17511268/cant-enable-pojo-based-json-binding-support-for-jackson-in-jersey-2-0. – Rob Bygrave Jan 19 '14 at 22:52
4

I highly recommend JBoss RESTEasy for the REST API. I've used it on a couple of projects and found it to be trivial to setup. It also integrates nicely with Spring if you need that.

I have used both Jackson and Gson for the JSON support with RESTEasy and it is quite simple. All you do is annotate a POJO with JAXB annotations and include the proper libraries.

Another really great part of RESTEasy is the good support for multipart form data. They provide an @MultipartForm annotation which allows you to bind a multipart form to a POJO without writing any code...works slick.

I would advise against Spring MVC for REST because it is not JAX-RS compliant. Using a JAX-RS compliant interface gives you a little bit better portability if you decide to switch to a different implementation down the road.

Matt Accola
  • 4,090
  • 4
  • 28
  • 37
  • Hmm, I'm not sure, it looks like it expects to be deployed in a war file, and I would rather avoid a heavyweight servlet container and all of that infrastructure, the API is just a small part of my project, I don't want to stuff the entire thing in a war just so I can support the API :-/ – sanity Feb 29 '12 at 18:30
  • Run it on Jetty...it will be light-weight! – Matt Accola Mar 01 '12 at 16:38
3

for converting json to pojos : gson and jackson . For Restful I'd use spring, or restlet.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • Yes, I'm very familiar with Gson, and Jersey already uses Jackson, but converting JSON to and from POJOs is only one part of the problem, I'm looking for an alternative to Jersey, which is a framework for building REST-HTTP APIs. If one existed I'd prefer a HTTP framework that worked with Gson instead of Jersey as I'm more comfortable with it. – sanity Feb 29 '12 at 17:13
  • Crisp clean answer . This is what i was looking for – mohammed nathar Oct 04 '18 at 09:51
  • SpringBoot all the way :D – ReyAnthonyRenacia May 17 '19 at 05:05
3

Note: I'm the EclipseLink JAXB (MOXy) lead, a member of the JAXB (JSR-222) expert group and a contributor to the Jersey project.

With Jersey or any JAX-RS project you can use a MessageBodyReader/MessageBodyWriter for complete control over your JSON binding. Below is an example of using this mechanism to leverage MOXy as the JSON provider.

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
1

This is a bias answer, but we have been using Snow for all our enterprise & social application and it is extremely lightweight, fully based on Guice, and allows you to bind any REST call to any Java Method (with the object managed by Guice). Very flexible and simple.

http://britesnow.com/snow

I am the creator of this micro framework, so, I would love to get your feedback.

Jeremy Chone
  • 3,079
  • 1
  • 27
  • 28
0

You can try JEST, it works over OpenJPA.

thSoft
  • 21,755
  • 5
  • 88
  • 103
0

As NimChimpsky mentioned restlet will do this out of the box. Furthermore, if your client is also java you can abstract the functionality into an interface and wrap it with a client side proxy fairly easily. This tutorial page shows how to do what I've mentioned here.

Community
  • 1
  • 1
javamonkey79
  • 17,443
  • 36
  • 114
  • 172