HTTP请求结果的不同编码,取决于Accept标头 [英] Different encoding of an HTTP request result, depending on the Accept header

查看:430
本文介绍了HTTP请求结果的不同编码,取决于Accept标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 code> @RequestMapping(value =/documentation/{appId:.+}/,method = RequestMethod.POST)
@ResponseBody
public String uploadDocumentation(HttpServletRequest request,
@PathVariable String appId,@RequestParam(uploadedfile)MultipartFile文件)
throws异常{
// ....
String json = JsonUtils.jsonify(map);
if(accept(request,application / json)){
return json;
} else if(accepted(request,text / html)){
return< textarea> + json +< / textarea>;
} else {
throw new GinaException(Type de retour nonsupporté);
}

我想知道是否有办法在框架中注册此编码机制,所以我们只需要返回一个对象,让框架做其他的事情。



提前感谢

解决方案

为了纪录,我只是添加了第二个方法:

  @ RequestMapping(value =/documentation/{appId:.+}/,method = RequestMethod.POST,
produce =application / json)
@ResponseBody
public UploadResult uploadDocumentation(@ PathVariable String appId,
@RequestParam(uploadedfile)MultipartFile文件)throws异常{
...
返回新的UploadResult(filename);
}

@RequestMapping(value =/documentation/{appId:.+}/,method = RequestMethod.POST,
produce =text / html)
@ResponseBody
public String uploadDocumentationIE(@PathVariable String appId,
@RequestParam(uploadedfile)MultipartFile文件)throws异常{
UploadResult obj = uploadDocumentation(appId,file);
String json = JsonUtils.jsonify(obj);
return< textarea> + json +< / textarea>;
}


I have a controller with a method to upload files, using, on the client side, the dojo Uploader class that supports ajax uploads for all browsers except IE, and uploads with an IFrame for IE. The result is a JSON object, but when the IFrame mechanism is used, the JSON must be enclosed in a <textarea>:

@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é");
    }

I was wondering if there is a way to register this encoding mechanism in the framework, so that we would just have to return an object, and let the framework do the rest.

Thanks in advance.

解决方案

For the record, I simply added a second method:

@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>";
}

这篇关于HTTP请求结果的不同编码,取决于Accept标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆