Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A better alternative to Jersey/Jackson for building JSON REST APIs? [closed]

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?

like image 897
sanity Avatar asked Feb 29 '12 17:02

sanity


People also ask

Does JAX-RS use Jackson?

Open Liberty's JAX-RS 2.0 implementation uses Jackson as its default JSON provider.

Does jersey use Jackson?

Jersey uses Jackson internally to convert Java objects to JSON and vice versa.

Do spring boots use jersey?

The spring-boot-starter-jersey is a starter for building RESTful web applications using JAX-RS and Jersey. It is an alternative to spring-boot-starter-web . The spring-boot-starter-test is a starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito.

How do I send a JSON response to a jersey?

Jersey endpoints and return a JSON responseCreate a few endpoints in Jersey, and Jackson will handle the object from/to JSON conversion. 4.1 Create the following endpoints and return JSON response. GET /json/ , returns a JSON string. GET /json/{name} , returns an User object containg the {name} in JSON string.


3 Answers

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

like image 99
hakonlo Avatar answered Nov 10 '22 10:11

hakonlo


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.

like image 37
Matt Accola Avatar answered Nov 10 '22 09:11

Matt Accola


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

like image 3
NimChimpsky Avatar answered Nov 10 '22 10:11

NimChimpsky