zoukankan      html  css  js  c++  java
  • springmvc controller动态设置content-type

      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) {
        }
    }
  • 相关阅读:
    jQuery自定义选项卡插件
    jQuery委托事件delegate()方法
    发布/订阅模式
    Node.js + Nginx WNMP 多域名 多端口 反向代理
    让Nginx支持apk、ipa文件下载
    jQuery中bind方法传参
    Http协议详解
    vuecli2.X环境搭建
    vue绑定属性、绑定class及绑定style
    vue数据渲染、条件判断及列表循环
  • 原文地址:https://www.cnblogs.com/hujunzheng/p/9405178.html
Copyright © 2011-2022 走看看