最近仕事でSpringBootに触れる機会があったので、便利だと思ったのを書いていこうと思います。
前提
以下のFORMを定義しておきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package jp.doraliver.form; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class SampleForm { private String hoge; private String fuga; private String piyo; } |
@Date(Lombok)いうアノテーションでSetter/Getterを実装しています。
SpringBootはアノテーションがかなり便利なので使っていきましょう!
GETメソッド
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package jp.doraliver.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import jp.doraliver.form.SampleForm; @Controller public class SampleFormGet { @ResponseBody @RequestMapping(value = "/sample", method = RequestMethod.GET) public SampleForm get(@ModelAttribute SampleForm form) { return form; } } |
この状態でSpringBootを起動し、以下のアドレスを入力します。
http://localhost:8080/sample?hoge=%E3%83%89%E3%83%A9%E3%83%AA%E3%83%90&fuga=%E3%81%AE&piyo=%E5%82%99%E5%BF%98%E9%8C%B2
すると…
見事に受け取ってjsonで返してくれました。
SpringBootはDTOなどを返すだけでjsonに変換してくれるので非常にありがたいですね!
POSTメソッド – application/json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package jp.doraliver.controller; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import jp.doraliver.form.SampleForm; @Controller public class SampleFormPostJson { @ResponseBody @RequestMapping(value = "/sample", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public SampleForm get(@RequestBody SampleForm form) { return form; } } |
POSTメソッドのContent-Typeにapplication/jsonを指定するとこちらに来ます。
GETと同じく自動的にFORMへ格納してくれます。
GETと異なるのは主に@ModelAttributeだったところが@RequestBodyに変わったのが重要です。
@RequestBodyだとリクエストボディから値を取得してくれるようです。
POSTメソッド – application/x-www-form-urlencoded
主にHTMLのFORMタグで送信される際に使われますね。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package jp.doraliver.controller; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import jp.doraliver.form.SampleForm; @Controller public class SampleFormPostForm { @ResponseBody @RequestMapping(value = "/sample", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) public SampleForm get(@RequestBody MultiValueMap<String, String> values) { SampleForm form = new SampleForm(); form.setHoge(values.getFirst("hoge")); form.setFuga(values.getFirst("fuga")); form.setPiyo(values.getFirst("piyo")); return form; } } |
MultiValueMapというのは一つのキーで複数の値を保持できるマップのようです。
基本的に一個であればgetFirstメソッドで十分ですね。
リクエストヘッダ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package jp.doraliver.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import jp.doraliver.form.SampleForm; @Controller public class SampleFormGet { @ResponseBody @RequestMapping(value = "/sample", method = RequestMethod.GET) public SampleForm get(@ModelAttribute SampleForm form, @RequestHeader("Content-Type") String contentType) { System.out.println(contentType); return form; } } |
リクエストヘッダはDTOに変換する仕組みはないみたいですね。
他にも使えそうなのがあり次第追記していこうかと思います。
まとめ
- GETメソッドは@ModelAttribute
- POSTメソッドは@RequestBody
- ヘッダは@RequestHeader