📝 write()
メソッドの場合(レスポンス書き込み)
@Override
protected void writeInternal(String str, HttpOutputMessage outputMessage)
throws IOException {
Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType());
OutputStream out = outputMessage.getBody();
StreamUtils.copy(str, charset, out);
}
-
str
がnull
の場合 →StreamUtils.copy(str, ...)
でNullPointerException
が発生します。 - このため、
write()
の前に呼び出し側がnull
チェックしていないと、実行時に NPE(NullPointerException) が発生します。
✅ 対策
-
@ResponseBody
やHttpEntity<String>
を返すような処理でnull
を返す可能性がある場合は、明示的に空文字列""
を返すようにするのが安全です。 - カスタムの
HttpMessageConverter
を定義してnull
を許容することも可能ですが、一般的には避けた方が無難です。