LoginSignup
0
2

More than 5 years have passed since last update.

Spring Web MVC @RequestBody で空のリクエストを受けるには

Posted at

@RequestBody には required 属性があって、そのデフォルト値は true です。

例えば以下のような API が定義されていて(リクエストをそのまま返す API です)、これに対して「空」のリクエストを送った場合、デフォルト値のままだと、org.springframework.http.converter.HttpMessageNotReadableException が発生します。

@PostMapping(path = "/hogehoge",
  consumes = "text/plain; charset=UTF-8",
  produces = "text/plain; charset=UTF-8")
@ResponseBody
public String execute(@RequestBody String body) {
  return body;
}

@RequestBody(required=false) とすることで、空のリクエストも処理できるようになります。ちなみに空のリクエストの場合、body には null が入ります。

  • Java 8
  • Spring Web MVC 4.3.6
0
2
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
2