SpringBoot リクエストマッピング
・リクエストマッピングは以下の条件を指定できる
value(path)
リクエストパス
method
HTTPメソッドによる絞り込み
params
リクエストパラメータによる絞り込み
headers
リクエストヘッダによる絞り込み
consumes
Content-Typeヘッダによる絞り込み
produces
Acceptヘッダによる絞り込み
・HTTPメソッドのマッピング確認
//@RestController //@RequestMapping("api/users") @Controller public class UserController { //@GetMapping("/") @RequestMapping(value="/", method=RequestMethod.GET) public ModelAndView index(ModelAndView mv) { mv.setViewName("index"); return mv; } //@PostMapping @RequestMapping(method=RequestMethod.POST) @ResponseBody public String createUser(@RequestParam("name") String name) { return "call post"; } //@GetMapping("/{id}") @RequestMapping(value="/{id}", method=RequestMethod.GET) @ResponseBody public String getUser(@PathVariable String id) { return "call get"; } //@PutMapping("/{id}") @RequestMapping(value="/{id}", method=RequestMethod.PUT) @ResponseBody public String updateUser(@PathVariable String id, @RequestParam String name, @RequestParam String password) { return "call put"; } //@DeleteMapping("/{id}") @RequestMapping(value="/{id}", method=RequestMethod.DELETE) @ResponseBody public String deleteUser(@PathVariable String id) { return "call delete"; } }
・PostMapping
<form method="post" action="/"> <label for="name">名前:</label> <input type="text" name="name"><br> <label for="password">パスワード:</label> <input type="text" name="password"> <input type="submit" value="送信" /> </form>
・GetMapping
<form method="get" action="/1"> <label for="name">名前:</label> <input type="text" name="name"><br> <label for="password">パスワード:</label> <input type="text" name="password"> <input type="submit" value="送信" /> </form>
・PutMapping
<form method="post" action="/1"> <label for="name">名前:</label> <input type="text" name="name"><br> <label for="password">パスワード:</label> <input type="text" name="password"> <input type="hidden" name="_method" value="PUT"> <input type="submit" value="送信" /> </form>
・DeleteMapping
<form method="post" action="/1"> <label for="name">名前:</label> <input type="text" name="name"><br> <label for="password">パスワード:</label> <input type="text" name="password"> <input type="hidden" name="_method" value="DELETE"> <input type="submit" value="送信" /> </form>