HttpServletResponse response.setContentType 변경 후
response.setContentType("application/octet-stream");
response.setContentType("application/octet-stream") 메서드는 응답 본문의 타입을 설정합니다.
이 설정은 response.getWriter() 또는 response.getOutputStream()과 같은 메서드를 사용하여 응답 본문을 생성하기 전에 수행되어야 합니다.
하지만, Return 에서 JSON 형태의 문자열을 생성하고 반환하므로, response.getWriter() 또는 response.getOutputStream() 메서드가 호출되기 전에 이미 응답 본문이 생성되어 버립니다.
따라서 response.setContentType("application/octet-stream") 설정이 무시되고, 기본적으로 Spring MVC가 제공하는 MappingJackson2JsonView가 생성하는 Content-Type인 application/json이 설정되는 것이라고 한다.
만약 response.setContentType("application/octet-stream") 설정을 유지하면서 JSON 형태의 응답 본문을 생성하고 싶다면, response.getWriter() 또는 response.getOutputStream() 메서드를 사용하여 응답 본문을 생성하고 난 후에 response.setContentType("application/octet-stream")을 설정해주어야 한다고 한다.
response.setContentType(); 이후 에러를 던졌을 때 왜 적용이 안되나 궁금했었다.