StringJSON を表すがあります。
[{"type":"ERROR","message":"some error message"}]
これは、次のコードでString返されます。ex.contentUTF8()
@RestControllerAdvice
@RequiredArgsConstructor
public class ControllerExceptionHandler {
@ExceptionHandler(FeignException.class)
@ResponseBody
public String afterThrowing(FeignException ex, HttpServletResponse response) {
response.setStatus(ex.status());
return ex.contentUTF8();
}
}
ただし、HTTP クライアントは JSON 応答を取得しません。代わりに、レスポンスの本文が that である HTML を取得しますString。代わりに JSON を返すにはどうすればよいですか? それを逆シリアル化する必要がありStringますか?
アップデート
Stringクライアントへの応答は、によって返される (シリアル化される) を表す JSON オブジェクトである必要がありますex.contentUTF8()。
[{"type":"ERROR","message":"some error message"}]
したがって、ErrorDto(いくつかの回答で提供されているように)のようなラッパーオブジェクトは機能しません。