【问题标题】:Grails file uploading using restful-api plugin使用 restful-api 插件上传 Grails 文件
【发布时间】:2015-09-09 10:49:28
【问题描述】:

我的 Config.groovy 文件内容如下:

resource 'ktm-documents' config{
serviceName ='docCompositeService'
// methods = ['list']
representation {
    mediaTypes = ["application/vnd.hedtech.v1+json", "application/json", "multipart/form-data"]
    marshallers{
        jsonBeanMarshaller{
            supports net.hedtech.banner.decorator.DocDecorator
        }
        jsonBeanMarshaller{
            supports net.hedtech.banner.decorator.Indexes
        }
    }
    extractor = new  net.hedtech.banner.extractor.DocExtractor()
}
}

提取器是:

class DocExtractor implements RequestExtractor{
@Override
Map extract(HttpServletRequest request) {
print("========== My EXTRACTOR ========")
MultipartHttpServletRequest mpr
Map responseMap = [:]
print request

try {
   print "Here's abc " + request.getParameter("abc")

   mpr = (MultipartHttpServletRequest) request;
   CommonsMultipartFile file = (CommonsMultipartFile) mpr.getFile("file");


    if (file.isEmpty()) {
        responseMap.put("error","file empty hai")
    } else {
        responseMap.put("file", file)
    }

print responseMap.values()

} catch (Exception e) {
   e.printStackTrace()
}
}
}

当尝试在服务的 create() 中的提取器中提取从请求中提取的文件时,我收到 “无法获取 null 的属性 'file'” 错误。

服务中的create()方法:

def create(Map params){

print params
File f = params.file

return ["File Uploaded!"]
}

请问有什么想法吗?

【问题讨论】:

    标签: rest grails plugins groovy


    【解决方案1】:

    代码没有问题。

    从 POSTMAN REST 客户端发送的请求中声明了 Content-Type

    请注意,即使在通过 multipart/form-data 请求发送时,我们也不应提及 Content-Type

    成功了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多