Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 11. Giving Spring some REST > Representing resources - Pg. 287

Representing resources throw new BindException(result); } spitterService.saveSpittle(spittle); 287 Set resource location response.setHeader("Location", "/spittles/" + spittle.getId()); Return Spittle resource return spittle; } The first thing you may notice is that this method's @RequestMapping is different from the ones we've seen so far. Unlike the others, this one doesn't have its value attribute set. That means that the controller's class-level @RequestMapping is solely responsible for determining the URL pattern handled by createSpittle() . More specifically, createSpittle() will handle requests whose URL pattern matches /spittles. Typically, the server determines a resource's identity. Since we're creating a new resource here, there's no way that we could know the URL for that resource. So, whereas GET , PUT , and DELETE requests operate directly on the resource identified by their URL , POST has to operate against a URL that isn't the same as the resource it's creating (because that URL won't exist until the resource is created). Once again, this method is annotated with @ResponseStatus to set the HTTP status code in the request. This time, the status will be set to 201 (Created) to indicate that a resource was successfully created. When an HTTP 201 response is returned to the cli-