【问题标题】:Different encoding of an HTTP request result, depending on the Accept headerHTTP 请求结果的不同编码,取决于 Accept 标头
【发布时间】:2014-10-31 08:19:43
【问题描述】:

我有一个控制器,它带有上传文件的方法,在客户端使用 dojo Uploader 类,该类支持除 IE 之外的所有浏览器的 ajax 上传,并使用 IE 的 IFrame 上传。 结果是一个 JSON 对象,但是当使用 IFrame 机制时,JSON 必须包含在

@RequestMapping(value = "/documentation/{appId:.+}/", method = RequestMethod.POST)
@ResponseBody
public String uploadDocumentation(HttpServletRequest request,
        @PathVariable String appId, @RequestParam("uploadedfile") MultipartFile file)
        throws Exception {
    // ....
    String json = JsonUtils.jsonify(map);
    if (accepts(request, "application/json")) {
                return json;
    } else if (accepts(request, "text/html")) {
        return "<textarea>" + json + "</textarea>";
    } else {
        throw new GinaException("Type de retour non supporté");
    }

我想知道是否有办法在框架中注册这种编码机制,这样我们只需要返回一个对象,然后让框架完成其余的工作。

提前致谢。

【问题讨论】:

    标签: spring spring-mvc dojo


    【解决方案1】:

    为了记录,我只是添加了第二种方法:

    @RequestMapping(value = "/documentation/{appId:.+}/", method = RequestMethod.POST,
            produces="application/json")
    @ResponseBody    
    public UploadResult uploadDocumentation(@PathVariable String appId,
             @RequestParam("uploadedfile") MultipartFile file) throws Exception {
        ...
        return new UploadResult(filename);
    }
    
    @RequestMapping(value = "/documentation/{appId:.+}/", method = RequestMethod.POST,
            produces="text/html")
    @ResponseBody    
    public String uploadDocumentationIE(@PathVariable String appId,
            @RequestParam("uploadedfile") MultipartFile file) throws Exception {
        UploadResult obj = uploadDocumentation(appId, file);
        String json = JsonUtils.jsonify(obj);
        return "<textarea>" + json + "</textarea>";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多