5

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!

5
  • JAX-WS is about SOAP, JAX-RS is about REST. Commented Jun 13, 2013 at 9:40
  • 1
    Maybe "jersey" is a good keyword to start with for you. Commented Jun 13, 2013 at 9:41
  • @Tichodroma Thank you, there are so many acronyms in Java world :) Commented Jun 13, 2013 at 10:09
  • @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. Commented Jun 13, 2013 at 10:11
  • Added some code to my answer. :) Commented Jun 13, 2013 at 12:19

3 Answers 3

3

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
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! The other thing is how do you separate domain objects from objects exposed to API? How would you version API?
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}. :)
2

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.

Comments

2

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

3 Comments

I'm interested in server side of thing. Not much client side.
@JakubKulhan See the 3rd link. You have there a full working REST server Jersey example with Tomcat as server and Android on client side.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.