0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Spring BootでJSON形式のリクエストを送ったらHTTP415errorになった際の原因と対処法

Last updated at Posted at 2023-12-22

事象

SpringBootを使って実装したサーバーにJSON形式のcurlコマンドを投げたところ「HTTP 415エラー(415 Unsupported Media Type)」になった。

解決策

コントローラメソッドのリクエストボディの型をMultiValueMapからMapに変更したところ解消した。

修正後
@PostMapping(value = "/a-resource", consumes = MediaType.APPLICATION_JSON)
public Object example(@RequestBody Map<String, String> request){/*処理*/}

原因

JSON形式のリクエストを受取った際、Spring内部でJacksonを用いてJSONをパースしているため、コントローラの型がMapであるべきのようだ。
コピペ元の引数がMultiValueMapだったのはapplication/x-www-form-urlencoded形式だったからであった。

なかなか正解にたどり着けなかったためシェア。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?