Moved REST HTTP Method Conversion to View chapter.
This commit is contained in:
parent
06777c8123
commit
e9ac4bc0ad
|
|
@ -38,50 +38,6 @@
|
|||
configuration</link> to understand the general programming model.</para>
|
||||
</section>
|
||||
|
||||
<section id="rest-method-conversion">
|
||||
<title>HTTP Method Conversion</title>
|
||||
|
||||
<para>A key principle of REST is the use of the Uniform Interface. This
|
||||
means that all resources (URLs) can be manipulated using the same four
|
||||
HTTP methods: GET, PUT, POST, and DELETE. For each methods, the HTTP
|
||||
specification defines the exact semantics. For instance, a GET should
|
||||
always be a safe operation, meaning that is has no side effects, and a
|
||||
PUT or DELETE should be idempotent, meaning that you can repeat these
|
||||
operations over and over again, but the end result should be the same.
|
||||
While HTTP defines these four methods, HTML only supports two: GET and
|
||||
POST. Fortunately, there are two possible workarounds: you can either
|
||||
use JavaScript to do your PUT or DELETE, or simply do a POST with the
|
||||
'real' method as an additional parameter (modeled as a hidden input
|
||||
field in an HTML form). This latter trick is what Spring's
|
||||
<classname>HiddenHttpMethodFilter</classname> does. This filter is a
|
||||
plain Servlet Filter and therefore it can be used in combination with
|
||||
any web framework (not just Spring MVC). Simply add this filter to your
|
||||
web.xml, and a POST with a hidden _method parameter will be converted
|
||||
into the corresponding HTTP method request.</para>
|
||||
|
||||
<section id="rest-form-tags">
|
||||
<title>Supporting Spring form tags</title>
|
||||
|
||||
<para>To support HTTP method conversion the Spring MVC form tag was
|
||||
updated to support setting the HTTP method. For example, the following
|
||||
snippet taken from the updated Petclinic sample</para>
|
||||
|
||||
<programlisting language="xml"><form:form method="delete">
|
||||
<p class="submit"><input type="submit" value="Delete Pet"/></p>
|
||||
</form:form></programlisting>
|
||||
|
||||
<para>This will actually perform an HTTP POST, with the 'real' DELETE
|
||||
method hidden behind a request parameter, to be picked up by the
|
||||
<classname>HiddenHttpMethodFilter</classname>. The corresponding
|
||||
@Controller method is shown below</para>
|
||||
|
||||
<programlisting language="java">@RequestMapping(method = RequestMethod.DELETE)
|
||||
public String deletePet(@PathVariable int ownerId, @PathVariable int petId) {
|
||||
this.clinic.deletePet(petId);
|
||||
return "redirect:/owners/" + ownerId;
|
||||
}</programlisting>
|
||||
</section>
|
||||
</section>
|
||||
|
||||
<section id="rest-etag">
|
||||
<title>ETag support</title>
|
||||
|
|
|
|||
|
|
@ -737,6 +737,57 @@ productList.url=/WEB-INF/jsp/productlist.jsp</programlisting>
|
|||
</tr>
|
||||
</form></programlisting>
|
||||
</section>
|
||||
|
||||
<section id="rest-method-conversion">
|
||||
<title>HTTP Method Conversion</title>
|
||||
|
||||
<para>A key principle of REST is the use of the Uniform Interface.
|
||||
This means that all resources (URLs) can be manipulated using the same
|
||||
four HTTP methods: GET, PUT, POST, and DELETE. For each methods, the
|
||||
HTTP specification defines the exact semantics. For instance, a GET
|
||||
should always be a safe operation, meaning that is has no side
|
||||
effects, and a PUT or DELETE should be idempotent, meaning that you
|
||||
can repeat these operations over and over again, but the end result
|
||||
should be the same. While HTTP defines these four methods, HTML only
|
||||
supports two: GET and POST. Fortunately, there are two possible
|
||||
workarounds: you can either use JavaScript to do your PUT or DELETE,
|
||||
or simply do a POST with the 'real' method as an additional parameter
|
||||
(modeled as a hidden input field in an HTML form). This latter trick
|
||||
is what Spring's <classname>HiddenHttpMethodFilter</classname> does.
|
||||
This filter is a plain Servlet Filter and therefore it can be used in
|
||||
combination with any web framework (not just Spring MVC). Simply add
|
||||
this filter to your web.xml, and a POST with a hidden _method
|
||||
parameter will be converted into the corresponding HTTP method
|
||||
request.</para>
|
||||
|
||||
<para>To support HTTP method conversion the Spring MVC form tag was
|
||||
updated to support setting the HTTP method. For example, the following
|
||||
snippet taken from the updated Petclinic sample</para>
|
||||
|
||||
<programlisting language="xml"><form:form method="delete">
|
||||
<p class="submit"><input type="submit" value="Delete Pet"/></p>
|
||||
</form:form></programlisting>
|
||||
|
||||
<para>This will actually perform an HTTP POST, with the 'real' DELETE
|
||||
method hidden behind a request parameter, to be picked up by the
|
||||
<classname>HiddenHttpMethodFilter</classname>, as defined in web.xml:</para>
|
||||
<programlisting language="java"><filter>
|
||||
<filter-name>httpMethodFilter</filter-name>
|
||||
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
|
||||
</filter>
|
||||
|
||||
<filter-mapping>
|
||||
<filter-name>httpMethodFilter</filter-name>
|
||||
<servlet-name>petclinic</servlet-name>
|
||||
</filter-mapping></programlisting><para>The corresponding @Controller method
|
||||
is shown below:</para>
|
||||
|
||||
<programlisting language="java">@RequestMapping(method = RequestMethod.DELETE)
|
||||
public String deletePet(@PathVariable int ownerId, @PathVariable int petId) {
|
||||
this.clinic.deletePet(petId);
|
||||
return "redirect:/owners/" + ownerId;
|
||||
}</programlisting>
|
||||
</section>
|
||||
</section>
|
||||
</section>
|
||||
|
||||
|
|
@ -2435,7 +2486,7 @@ simpleReport.reportDataKey=myBeanData</programlisting>
|
|||
</section>
|
||||
</section>
|
||||
|
||||
<section id="rest-feedview">
|
||||
<section id="view-feeds">
|
||||
<title>Feed Views</title>
|
||||
|
||||
<para>Both <classname>AbstractAtomFeedView</classname> and
|
||||
|
|
|
|||
Loading…
Reference in New Issue