springmvc??RequestMappingHandlerAdapter#invokeHandlerMethod?通過ServletInvocableHandlerMethod#invokeAndHandle調用目標方法,并處理返回值。
如果return?value !=?null,則通過returnvalueHandlers處理,內部會調用MessageConverter轉換成相應的報文格式。
HttpOutputMessage outputMessage?對應的實例?是 org.springframework.http.server.ServletServerHttpResponse。
在寫入數據的同時,會設置response的header,包括content-type(根據RequestMapping?的?produces?屬性?計算出來)。
所以,在帶有返回值的情況下,在controller中設置content-type是無效的,會被消息轉換器覆蓋掉。
@RequestMapping(value = "xxx", method = {RequestMethod.POST, RequestMethod.GET}) @ResponseBody public String handleKafkaSpecialMessage(HttpServletRequest request, HttpServletResponse response) {response.setContentType("application/json");return "xxx"; }
改一下返回值就好了
@RequestMapping(value = "xxx", method = {RequestMethod.POST, RequestMethod.GET}) @ResponseBody public void handleKafkaSpecialMessage(HttpServletRequest request, HttpServletResponse response) {response.setContentType("application/json");try(OutputStream ros = response.getOutputStream()) {IOUtils.write("xxx", ros);ros.flush();} catch (IOException e) {} }
?