25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12 ■ Exposing a REST API<br />

}<br />

@RequestMapping(value="/{id}",method=RequestMethod.DELETE)<br />

public @ResponseBody Document removeDocument(@PathVariable String id){<br />

return documentFacade.removeDocumentById(id);<br />

}<br />

Remember Chapter 10? You are doing the same thing here, defining the controller through the different<br />

annotations. The <strong>Spring</strong> <strong>Framework</strong> MVC extension facilitates the way you expose these resources or web services.<br />

The difference now is that you are using the @ResponseBody and @RequestBody annotations. The <strong>Spring</strong> MVC<br />

extension uses JSON (JavaScript Object Notation, a readable text format with key-value pairs) as the default format<br />

for the RESTful resources, so @ResponseBody will convert the result in a JSON format and @RequestBody will accept a<br />

Content-Type:"application/json" format. Also, you are using the @PathVariable that will match the variable name<br />

between curly braces and the name of the parameter within the method call.<br />

Listing 12-5 shows the XML configuration that you will use in the unit test. The web.xml is the same as the one in<br />

Chapter 10.<br />

Listing 12-5. WEB-INF/mydocuments-servlet.xml<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

175

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!