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?
Open Liberty's JAX-RS 2.0 implementation uses Jackson as its default JSON provider.
Jersey uses Jackson internally to convert Java objects to JSON and vice versa.
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.
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.
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
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.
for converting json to pojos : gson and jackson . For Restful I'd use spring, or restlet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With