Pivotal Certified Spring Web Application Developer Exam by Iuliana Cosmina

Pivotal Certified Spring Web Application Developer Exam by Iuliana Cosmina

Author:Iuliana Cosmina
Language: eng
Format: epub
Publisher: Apress, Berkeley, CA


URI templates.

Content negotiation.

Many message converters offer out-of-the-box support.

RestTemplate and AsyncRestTemplate classes are used for easily creating client applications or for testing RESTful application services.

Browsers are supported as clients, although HTTP method conversion is necessary for PUT and DELETE methods. When making REST requests from a web page, jQuery can be used (this is covered in Chapter 6).

A few of these have already been mentioned in the previous section, as they were involved in creating REST clients; the others are covered in this section.

To develop a RESTful service class with Spring MVC, you have to do the most obvious thing: create a controller that contains handler methods that return resources representations instead of views, which are the actual response body. In Spring 3.0, we had to do the following:

@Controller

@RequestMapping(value = "/rest-persons")

public class PersonsRestController {

@Autowired

PersonManager personManager;

@ResponseStatus(HttpStatus.OK)

@RequestMapping(value = "/id/{id}", method = RequestMethod.GET)

public@ResponseBody Person getPersonById(@PathVariable Long id)

throws NotFoundException {

Person person = personManager.findById(id);

if (person == null) {

throw new NotFoundException(Person.class, id.toString());

}

return person;

}

}

Looks like any MVC controller, right? The only difference is the @ResponseBody that indicates a method return value should be bound to the web response body. The advantage here is that, in the same controller you can also have methods that are not used to provide REST representations, having all the people management data in one place. But, because it is a good practice to decouple code with different scopes, in Spring MVC 4.0 the @RestController was introduced. This annotation is conveniently annotated with @Controller and @ResponseBody, which practically means that if you annotate a class with it, all handler methods are transparently annotated with @ResponseBody. Also, the purpose of this controller becomes quite obvious—it handles only REST requests. Thus, the preceding code becomes the following:

@RestController

@RequestMapping(value = "/rest-persons")

public class PersonsRestController extends BaseController {

@ResponseStatus(HttpStatus.OK)

@RequestMapping(value = "/id/{id}", method = RequestMethod.GET)

public Person getPersonById(@PathVariable Long id) throws NotFoundException {

... // identical content as above

}

}

And this is all. All methods defined inside this class can then be called from REST clients, and they will receive the requested representations. What happens in the background—the way that the DispatcherServlet is involved—is depicted in Figure 5-9.

Figure 5-9.Spring MVC RESTFul Container



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.