在AWS Lambda中运行时,Spring Boot应用程序会引发MultipartException

时间:2018-10-22 19:55:24

标签: java spring amazon-web-services spring-boot aws-lambda

我刚刚尝试使用以下文章在AWS Lambda上运行Spring Boot应用程序:https://github.com/awslabs/aws-serverless-java-container/wiki/Quick-start---Spring-Boot

基本请求运行正常,但是发送文件似乎会引起问题。在本地运行应用程序可以正常运行:

@RestController
@EnableWebMvc
public class PingController {
    @RequestMapping(path = "/ping", method = RequestMethod.GET)
    public Map<String, String> ping() {
        Map<String, String> pong = new HashMap<>();
        pong.put("pong", "Hello, World!");
        return pong;
    }

    @RequestMapping(path = "/ping", method = RequestMethod.POST)
    public Map<String, Long> ping(@RequestParam("file") MultipartFile file) {
        Map<String, Long> pong = new HashMap<>();
        pong.put("filesize", file.getSize());
        return pong;
    }
}

/ ping上的GET按预期返回有效消息:

{
    "pong": "Hello, World!"
}

将POST发送到包含图像的/ ping会导致以下异常:

{
    "timestamp": 1540237302941,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "org.springframework.web.multipart.MultipartException",
    "message": "Could not parse multipart servlet request; nested exception is java.lang.IllegalArgumentException: File path not allowed: /image.jpeg",
    "path": "/ping"
}

application.properties文件中添加以下行无济于事:

spring.http.multipart.location=/tmp/

指定资源是否存在甚至都没有关系。到/ ping2的POST(不存在)会导致相同的异常。

CloudWatch的Stacktrace:

2018-10-22 19:47:21.701 ERROR 1 --- [ main] o.s.boot.web.support.ErrorPageFilter : Forwarding to error page from request [/ping] due to exception [Could not parse multipart servlet request; nested exception is java.lang.IllegalArgumentException: File path not allowed: /image.jpeg]

org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalArgumentException: File path not allowed: /image.jpeg
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:112) ~[task/:na]
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:86) ~[task/:na]
at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:80) ~[task/:na]
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1104) ~[task/:na]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:936) ~[task/:na]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[task/:na]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[task/:na]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) ~[task/:na]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) ~[task/:na]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[task/:na]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) ~[task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.FilterChainManager$ServletExecutionFilter.doFilter(FilterChainManager.java:351) ~[task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder.doFilter(FilterChainHolder.java:84) [task/:na]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[task/:na]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder.doFilter(FilterChainHolder.java:84) [task/:na]
at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:115) [task/:na]
at org.springframework.boot.web.support.ErrorPageFilter.access$000(ErrorPageFilter.java:59) [task/:na]
at org.springframework.boot.web.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:90) [task/:na]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [task/:na]
at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:108) [task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder.doFilter(FilterChainHolder.java:84) [task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.AwsLambdaServletContainerHandler.doFilter(AwsLambdaServletContainerHandler.java:207) [task/:na]
at com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler.handleRequest(SpringBootLambdaContainerHandler.java:154) [task/:na]
at com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler.handleRequest(SpringBootLambdaContainerHandler.java:52) [task/:na]
at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxy(LambdaContainerHandler.java:168) [task/:na]
at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxyStream(LambdaContainerHandler.java:200) [task/:na]
at com.example.StreamLambdaHandler.handleRequest(StreamLambdaHandler.java:31) [task/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at lambdainternal.EventHandlerLoader$StreamMethodRequestHandler.handleRequest(EventHandlerLoader.java:350) [LambdaSandboxJava-1.0.jar:na]
at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888) [LambdaSandboxJava-1.0.jar:na]
at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:292) [LambdaSandboxJava-1.0.jar:na]
at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64) [LambdaSandboxJava-1.0.jar:na]
at java.lang.Class.forName0(Native Method) [na:1.8.0_181]
at java.lang.Class.forName(Class.java:348) [na:1.8.0_181]
at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:94) [LambdaJavaRTEntry-1.0.jar:na]
Caused by: java.lang.IllegalArgumentException: File path not allowed: /image.jpeg
at com.amazonaws.serverless.proxy.internal.SecurityUtils.getValidFilePath(SecurityUtils.java:192) ~[task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequest.getMultipartFormParametersMap(AwsProxyHttpServletRequest.java:714) ~[task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequest.getParts(AwsProxyHttpServletRequest.java:305) ~[task/:na]
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:93) ~[task/:na]
... 38 common frames omitted

之前有人遇到过错误吗?我还需要配置其他内容吗?

1 个答案:

答案 0 :(得分:1)

这是Amazon库中的错误,已于几天前修复。 here提供了更多信息。只需确保使用1.2版或更高版本即可:

<dependency>
    <groupId>com.amazonaws.serverless</groupId>
    <artifactId>aws-serverless-java-container-spring</artifactId>
    <version>1.2</version>
</dependency>