SpringMVC入門-RESTful Web Serviceを作成するメモ

1.RestControllerコード
@Controller
@RequestMapping(“/rest")
public class RestController {
@RequestMapping(value="/user/{id}",method=RequestMethod.GET)
public String get(@PathVariable(“id") Integer id){
System.out.println(“get"+id);
return “/hello";
}

@RequestMapping(value="/user/{id}",method=RequestMethod.POST)
public String post(@PathVariable(“id") Integer id){
System.out.println(“post"+id);
return “/hello";
}

@RequestMapping(value="/user/{id}",method=RequestMethod.PUT)
public String put(@PathVariable(“id") Integer id){
System.out.println(“put"+id);
return “/hello";
}

@RequestMapping(value="/user/{id}",method=RequestMethod.DELETE)
public String delete(@PathVariable(“id") Integer id){
System.out.println(“delete"+id);
return “/hello";
}

}

2.web.xmlの設定
formでputとdeleteリクエストを送信します。
<!– configure the HiddenHttpMethodFilter,convert the post method to put or delete –>
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

3.JSPコード
<form action="rest/user/1″ method="post">
<input type="hidden" name="_method" value="PUT">
<input type="submit" value="put">
</form>
<form action="rest/user/1″ method="post">
<input type="submit" value="post">
</form>
<form action="rest/user/1″ method="get">
<input type="submit" value="get">
</form>
<form action="rest/user/1″ method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="delete">
</form>

Java

Posted by arkgame