In association with heise online

REST everywhere

To understand Spring 3's new features in the web and REST (Representational State Transfer; see glossary) areas, it is worth taking a quick look at the corresponding changes in Spring 2.5. The following listing shows a web controller, exclusively based on annotations.

@Controller
public class MyController {

@RequestMapping("/removeBook")
public String removeBook(
@RequestParam("book") String bookId) {
// do something meaningful
return "redirect:myBooks";
}
}

The listing shows that @Controller annotates the class. If Spring is configured correctly, this can be used to automatically create a Spring bean of this class. The annotation @RequestMapping defines the URL at which the annotated method will be called. It allows individual HTTP request parameters to be read out, with annotations for method parameters like @RequestParam. In our case, the method returns the name of the next view, which in this case is a redirect. This approach simplifies the task of developing web controllers.

The REST architecture has recently become prevalent in the web arena. It addresses business objects like customers via URLs. Different HTTP requests can be sent to these URLs. A POST, for example, can change an object's data, and a GET can be used to return the data. In addition, HTTP supports less commonly used methods like DELETE for deleting.

The new Spring attends to this area. Its first milestones already contain quite a bit of information, so it is worth taking a closer look at the underlying technology.

@RequestMapping(value = "/reward/{id}", method = GET)
public Reward show(@PathVariable("id") long id) {
return rewardsAdminService.findReward(id);
}

The listing above shows an example. Essentially, the features offered by Spring 2.5 have been adopted and extended in various places. Our example responds to a HTTP GET method. The URL uses variable components and is therefore called a URI template. The procedure involves assigning a part of the URL to a variable. For the URL "/reward/42", the id variable would be bound to the value of "42", and this very variable can then be referenced in the method's implementation, by attaching it to one of the method's parameters using @PathVariable.

The result of the method is a Reward. Spring MVC displays it in the usual way via HTML. However, REST generally offers to represent data in various different ways. JSON, XML, Atom and RSS are also commonly used in addition to HTML. Spring 3 not only introduces new views, it also offers to automatically select a suitable display method. This can be done in one of two basic ways: Either the accepts HTTP header lists the available display methods, for example accepts application/xml, or an extension, that is attached to the URL, so the GET, is sent to "/reward/42.xml" to return an XML view. Because XML support is required, the Object/XML mapping abstraction, which provides a common interface for technologies like JAXB and Xstream, will be moved into the Spring Framework from the Spring Web Services project.

ETag support is another new feature in Spring 3.0. This mechanism serves for sending a "no changes" message, instead of full reply, in a HTTP request. To do this, every HTTP reply includes an ETag value, which can be sent back with the next HTTP request. The server will then send, either updated data, or a "no changes" reply. This makes it much easier to support such scenarios efficiently.

Next: A matter of expression

Print Version | Permalink: http://h-online.com/-746537
  • Twitter
  • Facebook
  • submit to slashdot
  • StumbleUpon
  • submit to reddit
 


  • July's Community Calendar





The H Open

The H Security

The H Developer

The H Internet Toolkit