導入
休息は表現状態の転送です。 (リソース)パフォーマンスレイヤー状態変換。現在、最も人気のあるインターネットソフトウェアアーキテクチャです。明確な構造があり、標準に準拠し、理解しやすく、拡張しやすいため、ますます多くのWebサイトで採用されており、それぞれ削除、配置、およびCRUDに対応しています。 Spring3.0は、RESTスタイルのリクエストをサポートし始め、org.springframework.web.filter.hiddenhttpmethodfilterを介して、投稿リクエストをプットおよび削除リクエストに変換しました。この実験では、春4.0を使用しています。
hiddenhttpmethodfilterソースコード
public static final string default_method_param = "_method";@override保護されたvoid dofilterinternal(httpservletrequest request、httpservletresponse応答、フィルターチェーンフィルターチェーン)Srevletexception、ioexception、ioexception {string paramue = request.getParameter ( "post" .equals(request.getmethod())&& stringutils.haslength(paramvalue)){string method = paramvalue.touppercase(locale.english); httpservletrequest wrapper = new httpmethodrequestwrapper(request、request、request、method); {filterchain.dofilter(request、response);}}hiddenhttpmethodfilterのソースコードから、リクエストの_methodパラメーターに従ってSpringがそれを変換することがわかります。したがって、レストスタイルの削除またはリクエストを開始する場合は、_Methodパラメーターをフォームに含め、_Methodの値を削除または配置(Caps)に設定する必要があります。詳細な例は次のとおりです。
web.xmlでhiddenhttpmethodfilterを構成します
ハンドラーコードの書き込みページを書き込みます
<! - org.springframework.web.filter.hiddenhttpmethodfilterを設定して、投稿リクエストをPutまたは削除リクエストに変換するように - > <filter> <filter-name> hiddenhttpmethodfilter </filter-name> <filter-class> org.fil.hiddenhhhtpmethfttptpltpltpltpltptpmethwwork.filttpmethwwork. <フィルターマッピング> <filter-name> hiddenhttpmethodfilter </filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Rex.springmvc.handlers; Import org.apache.log4j.logger; Import org.springframework.tertype.controller; Import org.springframework.web.bind.annotation.Requestmapping; Import org.springframework.web.bind.Bind.Bind.Bind.Bind.Bind.Bind.Bind.Bind.Bind org.springframework.web.bind.annotation.RequestParam; @RequestMapping(value = "/retttest")@controllerpublic class resttestesthandler {private static final logger logger = logger.getLogger.getLogger.getLogger(RESTTESTHANDLER.CLASS); Private Static String " method = requestmethod.get)public string rectget(@requestparam(value = "id"、exicled = false)integer id){logger.debug( "rectget:" + id); return success;}@requestmapping(value = "/restput/{id}"、method = requestmethod.put)public string restint(fore forse = "id" id){logger.debug( "retsput:" + id); return success;}@requestmapping(value = "/retsdelete/{id}"、method = requestmethod.delete)public string restdelete(@requestdelete(@requestdelete(requestdelete)restdelete) success;}@requestmapping(value = "/retypost"、method = requestmethod.post)public string restpost(){logger.debug( "restpost"); return success;}}}<%@ page Language = "Java" contentType = "text/html; charset = utf-8" pageencoding = "utf-8"%> < "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> nofollow "> test rest get </a> <br> <form action =" retttest/retsput/1 "method =" post "> <input type =" hidden "name =" _ method "value =" put "> <input type =" submit "value =" submit "> </form> <br> <form action =" resttest/rectdelete/1 value = "delete"> <input type = "submit" "value =" submit "> </form> <br> <br> <form action =" retttest/restpost "method =" post "> <入力タイプ=" submit "value =" submit "> </form> </body> </html> </html> </html>
注:ハンドラーの@RequestParamアノテーションは、必要なパラメーターを使用して追加する必要があります。そうしないと、ページにアクセスするときに400エラーが発生します。
要約します
上記は、SpringMVCレストスタイルと実装コードの例の導入に関するこの記事のすべての内容です。私はそれが誰にでも役立つことを願っています。興味のある友達は引き続きこのサイトを参照できます:
springmvcの開始例
SpringMVC開発のユーザークエリコードの詳細な説明RestfulAPI
MultiPartFileを使用して非同期アップロードを実装するSpringMVCの方法の紹介
欠点がある場合は、それを指摘するためにメッセージを残してください。