将@GrailsCompileStatic检查请求强制为AbstractMultipartHttpServletRequest

时间:2017-05-28 20:10:54

标签: grails groovy multipart static-compilation

我尝试将@GrailsCompileStatic应用于具有从请求中检索MultipartFiles的操作的控制器:

request.getFile('foo')

但请注意以下事项:

  

[静态类型检查] - 找不到匹配方法   javax.servlet.http.HttpServletRequest#的GetFile(java.lang.String中)

是否有机会强制编译器验证针对AbstractMultipartHttpServletRequest(具有getFile(java.lang.String)方法)而不是HttpServletRequest的请求?

UPD 该解决方案有效:

MultipartFile multipartFile = ((StandardMultipartHttpServletRequest) request).getFile('myFile')

但在尝试测试时有一些奇怪的行为:

  

org.codehaus.groovy.runtime.typehandling.GroovyCastException:不能   施放物体   ' org.grails.plugins.testing.GrailsMockHttpServletRequest@2bcf856f'   与课程' org.grails.plugins.testing.GrailsMockHttpServletRequest'   上课   ' org.springframework.web.multipart.support.StandardMultipartHttpServletRequest'

1 个答案:

答案 0 :(得分:1)

http://docs.grails.org/2.2.1/api/org/codehaus/groovy/grails/plugins/testing/GrailsMockHttpServletRequest.html

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.html

都实现了一个接口

org.springframework.web.multipart.MultipartHttpServletRequest

所以只需使用此

import org.springframework.web.multipart.MultipartHttpServletRequest
...

MultipartFile multipartFile = ((MultipartHttpServletRequest) request).getFile('myFile')