Danny.Parker2002
Danny.Parker2002

Reputation: 65

Spring + (what library) for JSON REST API

I'm looking to make a REST API at work, and I'd like something that integrates well with Spring. I've looked at several and wanted to see if I missed anything and get the opinion of people more in the know.

So here are my basic requirements…

One more requirement…

Mapping to/from JSON. This has been the biggest stumbling point so far. I see a lot of frameworks supporting marshelling to/form JSON, but it is all straight POJO marshelling. As an example, I may want to provide a different representation of an object for different requests.

Say I want a list of all pet owners, I'd probably just want a list of people and their names, with a link to their pets. But if some other request was specifically for an owner, I'd want to list their pets (as children of the owner object) as part of the response.

Is there a good way to do this and define that kind of mapping, or do you just have to create POJOs with different annotations and transfer data from the entities to them? Basically creating a set of DTOs to represent the data for different requests?

Upvotes: 2

Views: 2504

Answers (2)

Neil McGuigan
Neil McGuigan

Reputation: 48246

  1. Create your entities (example Person.java)
  2. Use Spring Data JPA and create interfaces to auto-generate Repositories/DAOs for those entities (example. PersonRepository.java)
  3. Use Spring Data REST to export your Spring Data repositories as a HATEOAS/RESTful servlet (browse /persons url)

You don't have to write any code for the Repositories/DAOs. Spring Data JPA will write your DAO for you. All you have to do is create a very basic interface.

You don't have to write any controllers. Spring Data REST will export all of your Repositories using its own servlet.

While you're at it, use Hibernate's hbm2java to create your entities from your database schema, and create your Spring Data JPA repositories too.

Upvotes: 0

Biju Kunjummen
Biju Kunjummen

Reputation: 49915

Spring MVC probably meets all your requirements - here is a good reference - http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html

Upvotes: 2

Related Questions