web-dev-qa-db-ja.com

コンテンツタイプ 'text / plain; charset = UTF-8'はRestControllerクラス内のSpringBootでサポートされていないエラー

SpringBootアプリケーション内に次の@RestControllerを取得しました。

@Data
@RestController
public class Hello {

    @Autowired
    private ResturantExpensesRepo repo;

    @RequestMapping(value = "/expenses/restaurants",method = RequestMethod.POST,consumes =MediaType.APPLICATION_JSON_VALUE ,
            headers = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public void hello(@RequestBody ResturantExpenseDto dto)
    {
        Logger logger = LoggerFactory.getLogger("a");
        logger.info("got a request");

        ResturantExpenseEntity resturantExpenseEntity = new ResturantExpenseEntity();
        resturantExpenseEntity.setDate(new Date(System.currentTimeMillis()));
        resturantExpenseEntity.setName(dto.getName());
        resturantExpenseEntity.setExpense(dto.getExpense());
        repo.save(resturantExpenseEntity);
    }
}

RestClient/RestedClient(両方ともmozilaのアドオン)からリクエストを送信しようとすると、次のエラーが発生します。

{"timestamp":1512129442019、 "status":415、 "error": "Unsupported Media Type"、 "message": "Content type'text/plain; charset = UTF-8 'not support"、 "path": "/Expenses/restaurants "}

このエラーは、エンドポイントがJsonコンテンツをサポートしていないと述べていますが、私は

= MediaType.APPLICATION_JSON_VALUEを消費します

@RequestMappingアノテーション内

何が足りないのですか?

2
nadavgam

リクエストが次のように行われた場合、問題は解決します。

curl -X PUT -H 'Content-Type: application/json' -i http://localhost:8080/spring-rest/api/employees/500 --data '{
  "name": "abc",
  "email": "[email protected]",
  "salary": 10000
}'

ヘッダーが適切であることがわかります:headers = MediaType.APPLICATION_JSON_VALUE
ただし、リクエストが行われると、その時点で、そのアプリケーション/ jsonmimeタイプをハンドラーに通知する必要があります。

2
pkm

応答が遅れましたが、回答を投稿するときに同じ問題が発生したため、誰かに役立つ可能性があるため、Postmanをインストールしてから、Content-Typeをapplication/jsonに変更しました。

1
Tarek

これも遅いですが、RESTClient(Mozillaアドオン)では、[ヘッダー]ドロップダウンメニューからContent-Type:application/JSONを追加し、応答側でもJSON形式に変更できます。

0
Chin Hsu