Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing RESTful JSON API in Java

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!

like image 333
Jakub Kulhan Avatar asked Jun 13 '13 09:06

Jakub Kulhan


People also ask

Can you use JSON with REST API?

The REST architecture allows API providers to deliver data in multiple formats such as plain text, HTML, XML, YAML, and JSON, which is one of its most loved features.

How do I post JSON to a REST API endpoint in Java?

To post JSON to a REST API endpoint using Java, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the Java POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.

What is a RESTful JSON API?

A REST API (also known as RESTful API) is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services. REST stands for representational state transfer and was created by computer scientist Roy Fielding.


2 Answers

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
  }
}
like image 57
Enrichman Avatar answered Sep 30 '22 06:09

Enrichman


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.

like image 39
Machisuji Avatar answered Sep 30 '22 08:09

Machisuji