0
0

HTTP message body(POST、PUT、PATCHなど)のリクエストパラメータ照会

Posted at

HTTP message body

  • HTTP API で主に使用する。
  • データ形式はJSON(主に使用)、XML、TEXT などがある。
  • @RequestParam, @ModelAttributeを使用することができない。
  • ただし、HTML Form形式で配信される場合は、Request パラメータとして認められる。
  • @RequestBody(最も推薦) を使ってできる。
  • HTTP message body 情報を便利に照会することができる。
  • 自分で作ったオブジェクトを指定することもできる。
  • HTTP message converterがHTTP message bodyの内容を私たちが望む文字やオブジェクトなどに変換してくれる。
  • @RequestParam@ModelAttributeと違って省略してはならない。
    (省略すると@ModelAttributeが適用される。)

データ形式がJSONの場合

Untitled (4).png

@ResponseBody
@PostMapping("/request-body-json")
public HelloData requestBodyJson(@RequestBody HelloData helloData) throws IOException {

    log.info("helloData={}", helloData);

    return helloData;
}

2023-10-07 13:33:03.711  INFO 60746 --- [nio-8080-exec-2] h.s.b.request.RequestBodyJsonController  : helloData=HelloData(username=hello, age=20)
その他の方法1
private ObjectMapper objectMapper = new ObjectMapper();

@PostMapping("/request-body-json-v1")
public void requestBodyJsonV1(HttpServletRequest request, HttpServletResponse response) throws IOException {

    ServletInputStream inputStream = request.getInputStream();
    String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);

    log.info("messageBody={}", messageBody);

    HelloData helloData = objectMapper.readValue(messageBody, HelloData.class);

    log.info("helloData={}", helloData);

    response.getWriter().write("ok");
}

2023-10-07 13:26:37.198  INFO 60557 --- [nio-8080-exec-2] h.s.b.request.RequestBodyJsonController  : messageBody={"username":"hello", "age":"20"}
2023-10-07 13:26:37.222  INFO 60557 --- [nio-8080-exec-2] h.s.b.request.RequestBodyJsonController  : helloData=HelloData(username=hello, age=20)

データ形式が単純なテキストの場合

Untitled (5).png

@ResponseBody
@PostMapping("/request-body-string")
public String requestBodyString(@RequestBody String messageBody){
    log.info("messageBody={}", messageBody);

    return "ok";
}

2023-10-07 13:12:44.972  INFO 60141 --- [nio-8080-exec-2] h.s.b.r.RequestBodyStringController      : messageBody=simple text
その他の方法1
@PostMapping("/request-body-string-v1")
// public void requestBodyString(HttpServletRequest request, HttpServletResponse response) throws IOException {
public void requestBodyString(InputStream inputStream, Writer responseWriter) throws IOException {
    // ServletInputStream inputStream = request.getInputStream();
    String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);

    log.info("messageBody={}", messageBody);

    // response.getWriter().write("success");
    responseWriter.write("success");
}
その他の方法2
@PostMapping("/request-body-string-v3")
// public HttpEntity<String> requestBodyStringV2(HttpEntity<String> httpEntity){
public HttpEntity<String> requestBodyStringV2(RequestEntity<String> httpEntity){
    String messageBody = httpEntity.getBody();

    log.info("messageBody={}", messageBody);

    // return new HttpEntity<>("success");
		return new ResponseEntity<String>("success", HttpStatus.CREATED);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0