Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What is the idiomatic way of providing RESTful JSON API in Java? Do you use JAX-WS and XML annotations (@XmlElement etc.)? How do you serialize annotated objects to JSON (using Jackson or similar library)? How do you separate domain objects from objects sent out to API?

I know Java, I would like you to point me out to good resources and best practices about these topics.

Thank you!

share|improve this question
JAX-WS is about SOAP, JAX-RS is about REST. – Tichodroma 11 hours ago
Maybe "jersey" is a good keyword to start with for you. – Fildor 11 hours ago
@Tichodroma Thank you, there are so many acronyms in Java world :) – Jakub Kulhan 10 hours ago
@Fildor I have adopted a project that tries to provide REST API. However, it seems to me it does not solve the problem as it should, it needs refactoring. I try to seek best practices in this field. – Jakub Kulhan 10 hours ago
Added some code to my answer. :) – Enrichman 8 hours ago

3 Answers

I have good experience with Jersey and Jackson, even with Android, JBoss or Tomcat. See:

http://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/ and http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/

or

An Android REST Client and Tomcat REST Webservice

share|improve this answer
I'm interested in server side of thing. Not much client side. – Jakub Kulhan 9 hours ago
@JakubKulhan See the 3rd link. You have there a full working REST server Jersey example with Tomcat as server and Android on client side. – Bevor 9 hours ago
Ah, thank you! The other thing is the coupling of model objects and objects exposed in API. Currently the project I was given couples very tightly what is exposed in API to its internal objects. Breaking of the API is almost sure when something changes. How to decouple them? Two separate object hierarchies? Currently it would be just rewriting everything again. But in the future this hierarchies might change. – Jakub Kulhan 7 hours ago

I have used happily Jersey/JAX-RS but I would suggest you Spring MVC 3, not only for the rest api support but also for other interesting stuff as IoC or beans that could turn out to be useful.

Here a link where to refer: http://blog.springsource.org/2009/03/08/rest-in-spring-3-mvc/

Btw, I've used Jackson with Spring as parser. :)


A bit of code (basically mark your bean, as you said, with @XmlRootElement and use @Path to mark the API)

JAX-RS

bean:

@XmlRootElement
public class Response {

  private String result;
  private String message;

  //getter and setter
}

api:

@Path("rest/user")
@Produces(MediaType.APPLICATION_JSON)
public class UserService {

  @POST
  @Path("/login")
  public Response login(
        @FormParam("username") String username,
        @FormParam("password") String password
  ) {
      // Your logic here
  }
}

Spring

api:

@Controller
@RequestMapping("/user")
public class UserService {

  @RequestMapping(method = RequestMethod.POST, value="/login", headers="Accept=application/json")
  public @ResponseBody Response login(
        @RequestParam(value = "user", defaultValue = "") String email,
        @RequestParam(value = "password", defaultValue = "") String password,
        HttpServletRequest request
        ) {
    // Your logic here
  }
}
share|improve this answer
Thank you! The other thing is how do you separate domain objects from objects exposed to API? How would you version API? – Jakub Kulhan 7 hours ago
Usually I would put the domain object in a "model" package; if you have specific object for your api place them under the same package of the API. For the versioning the best practice would be to start always with version name, like /api/v1/{myApi}. :) – Enrichman 7 hours ago

I would simply use Play to save me a lot of work that has already been done. The link is for Play 1.2 and while the current version is 2.1, it should be fit for that as well.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.