The cause was that I tried to return JSON even though produces =" text / csv "
was set.
The situation is as follows.
--Return csv (set produces =" text / csv "
) Set endpoint
--At the time of aggregation error, the status is 400, and the error content is packed in JSON and returned.
Actually, the following annotations were added.
@PostMapping(value = "[end point]", produces = "text/csv")
--Status becomes 406 on error
--The error content is also empty
--On the server side, HttpMediaTypeNotAcceptableException
occurs
It was OK if the specification of produces
was removed.
@PostMapping(value = "[end point]")
If you do not specify produces
, it will be treated as good, so if there is no problem, you do not have to specify it It seems.
If you need to specify it dynamically and explicitly, you can write the response directly using HttpServletResponse
.
In that case, you can write as follows.
@PostMapping(value = "/api/csv")
public void csv(HttpServletResponse response) {
String csv = //CSV acquisition process;
response.setHeader(
"Content-Disposition",
"attachment;filename=\"" + /*file name*/ + "\""
);
response.setCharacterEncoding("Shift-JIS");
response.setContentType("text/csv");
try (Writer writer = response.getWriter()) {
writer.write(csv);
} catch (IOException e) {
//Error handling
}
}
Error handling was standardized using ʻExceptionHandler, but it took a long time to identify the cause without thinking that the
producesset in the controller would penetrate that far. It was written as
HttpMediaTypeNotAcceptableException` ...
-What happens if you specify produces of @RequestMapping -tokuhirom's blog -To write Response data directly in Spring -Qiita