Displaying data with RESTful web services in Spring

Spring provides RESTful web service implementations with its web MVC module. With each annotation, the creation of a REST web service is more or less like web MVC architecture. The RESTful web services can be built with the help of a REST controller. The noticeable difference between a web MVC and REST controller is the way they create the HTTP response.

A traditional web MVC uses various view technologies (such as JSP, Thymeleaf, and so on) to build a response, while the REST controller returns objects that are converted into JSON (or XML, based on the configuration), and finally sent as a HTTP response. For our Blogpress application, we will use RESTful services in the following two use cases:

  • Showing blog lists on the home page
  • Showing blog comments when a particular blog is open for view

To achieve this, we will write new controller class as follows:

@RestController
@RequestMapping("api")
public class BlogRESTController {

private Logger logger = LoggerFactory.getLogger(BlogRESTController.class);
@Autowired
private BlogService blogService;

@RequestMapping(value = "/listBlogs", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Blog>> getAllBlogJSON() {
logger.info("getting all blog data in json format ");
List<Blog> allBlogs = blogService.getAllBlogs();
return new ResponseEntity<List<Blog>>(allBlogs, HttpStatus.OK);
}

@RequestMapping(value = "/listAllComments", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Comment>> getAllCommentJSON() {
logger.info("getting all blog data in json format ");
List<Comment> allComments = blogService.getAllComments(0, 100);
return new ResponseEntity<List<Comment>>(allComments, HttpStatus.OK);
}
}

The REST controller must be defined with the @RestController annotation. Since we have two controllers now (one is the normal web MVC , and the second is the REST controller), we defined request mapping with @RequestMapping to differentiate the URL pattern.

The @RequestMapping annotation defines the method URL, HTTP method name and MIME type of the output this method produces. The getAllBlogJSON() method gets list of Blog objects and sends it with ResponseEntity, along with the HTTP response code. The ResponseEntity class represents the response body, header, and status code, and this class is used to prepare the HTTP response. To use it, the only thing required is to define it as return type of method (end point).

Alternatively, the @ResponseBody annotation (at method level) can be used to produce a HTTP response. ResponseEntity does exactly same as @ResponseBody , but provides some additional features, including setting the HTTP response code so it is better.

The ResponseEntity type is generic, so you can send any type of object with it. Both methods return the objects of Blog and Comment, respectively. Spring automatically converts the object list into a JSON string and returns it as a HTTP body. The MediaType class provides various mime types. The first method is accessible with thehttp://localhost:8080/api/listBlogs URL, and the second method with http://localhost:8080/api/listAllComments.

Next we will see how to present this data with a presentation layer. For our Blogpress application, we used the Thymeleaf template to construct a view layer. Thymeleaf templates are processed at server side. We will use another template engine called Mustache for client-side processing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.173.227