【发布时间】:2020-08-24 06:28:48
【问题描述】:
我正在尝试在 Spring Boot 中创建一个使用 JSON 的简单 Post 请求处理程序。
@RestController
@EnableWebMvc
public class WebFormController
{
@RequestMapping(path="/process-contact-form", method = RequestMethod.POST)
public ResponseEntity<String> processContact(@RequestBody Form form) throws Exception
{
System.out.println("TEST TEST: Ran");
return new ResponseEntity<String>("Thank you for contacting us, we'll respond soon.", HttpStatus.OK);
}
}
默认情况下,我的理解是这会消耗application/json。 但是,当我使用 curl 或 Postman 发出请求时,我会从 Spring 收到此响应...
DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported
在 Postman 中,我得到“415 Unsupported Media Type”。
如果我明确定义“消费”属性...
@RequestMapping(path="/process-contact-form", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
Spring Boot 返回这个...
DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported
我尝试将消耗设置为“application/json”、x-www-form-urlencoded 和纯文本,并从 Postman 发送这些类型的请求,但我得到了相同的结果。
我在 aws-serverless-java-container https://github.com/awslabs/aws-serverless-java-container/wiki/Quick-start---Spring-Boot 上构建了它,并从 SAM 本地 CLI 运行它。
编辑
我将日志记录切换到 DEBUG 以获取更多详细信息...
2020-05-08 17:53:40.692 DEBUG 1 --- [ main] s.b.w.s.f.OrderedCharacterEncodingFilter : Filter 'characterEncodingFilter' configured for use
2020-05-08 17:53:40.692 DEBUG 1 --- [ main] c.a.s.p.i.servlet.FilterChainHolder : Starting REQUEST: filter 0-characterEncodingFilter
2020-05-08 17:53:40.698 DEBUG 1 --- [ main] c.a.s.p.i.s.AwsProxyHttpServletRequest : Called set character encoding to UTF-8 on a request without a content type. Character encoding will not be set
2020-05-08 17:53:40.698 DEBUG 1 --- [ main] c.a.s.p.i.servlet.FilterChainHolder : Starting REQUEST: filter 2-com.amazonaws.serverless.proxy.internal.servlet.FilterChainManager$ServletExecutionFilter
2020-05-08 17:53:40.729 DEBUG 1 --- [ main] o.s.web.servlet.DispatcherServlet : POST "/process-contact-form", parameters={}
2020-05-08 17:53:40.732 DEBUG 1 --- [ main] c.a.s.p.i.servlet.AwsHttpServletRequest : Trying to access session. Lambda functions are stateless and should not rely on the session
2020-05-08 17:53:40.758 DEBUG 1 --- [ main] c.a.s.p.i.s.AwsHttpServletResponse : Response buffer flushed with 0 bytes, latch=1
2020-05-08 17:53:40.761 WARN 1 --- [ main] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported]
2020-05-08 17:53:40.761 DEBUG 1 --- [ main] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 415
2020-05-08 17:53:40.763 DEBUG 1 --- [ main] c.a.s.p.i.servlet.AwsHttpServletRequest : Trying to access session. Lambda functions are stateless and should not rely on the session
2020-05-08 17:53:40.768 DEBUG 1 --- [ main] c.a.s.p.i.servlet.FilterChainHolder : Executed ERROR: filter 3-com.amazonaws.serverless.proxy.internal.servlet.FilterChainManager$ServletExecutionFilter
2020-05-08 17:53:40.769 DEBUG 1 --- [ main] c.a.s.p.i.servlet.FilterChainHolder : Executed ERROR: filter 3-characterEncodingFilter
2020-05-08 17:53:40.779 INFO 1 --- [ main] c.a.s.p.internal.LambdaContainerHandler : 127.0.0.1:56893 null- null [08/05/2020:17:53:40Z] "POST /process-contact-form null" 415 - "-" "-" combined
2020-05-08 17:53:40.818 DEBUG 1 --- [ main] s.n.www.protocol.http.HttpURLConnection : sun.net.www.MessageHeader@c9d82f99 pairs: {POST /2018-06-01/runtime/invocation/807c7e06-86f6-1bd3-1055-78b464604573/response HTTP/1.1: null}{Docker-Lambda-Invoke-Wait: 1588960412475}{Docker-Lambda-Init-End: 1588960420587}{User-Agent: Java/1.8.0_201}{Host: 127.0.0.1:9001}{Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2}{Connection: keep-alive}{Content-type: application/x-www-form-urlencoded}{Content-Length: 104}
2020-05-08 17:53:40.822 DEBUG 1 --- [ main] s.n.www.protocol.http.HttpURLConnection : sun.net.www.MessageHeader@6f0129144 pairs: {null: HTTP/1.1 202 Accepted}{Content-Type: application/json}{Date: Fri, 08 May 2020 17:53:40 GMT}{Transfer-Encoding: chunked}
2020-05-08 17:53:40.825 DEBUG 1 --- [ main] s.n.www.protocol.http.HttpURLConnection : sun.net.www.MessageHeader@18fdb6cf5 pairs: {GET /2018-06-01/runtime/invocation/next HTTP/1.1: null}{User-Agent: Java/1.8.0_201}{Host: 127.0.0.1:9001}{Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2}{Connection: keep-alive}
编辑
StreamLambdaHandler
public class StreamLambdaHandler implements RequestStreamHandler {
private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
static {
try {
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
} catch (ContainerInitializationException e) {
// if we fail here. We re-throw the exception to force another cold start
e.printStackTrace();
throw new RuntimeException("Could not initialize Spring Boot application", e);
}
}
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
throws IOException {
System.out.println("TEST TEST " + inputStream.toString() + " " + outputStream.toString());
handler.proxyStream(inputStream, outputStream, context);
}
}
【问题讨论】:
-
您是否在 Postman 中设置了正确的内容类型?确保选择 Body -> raw -> JSON(在下拉菜单中)。
-
感谢 fram,我确实使用原始下拉菜单在 Postman 中设置了内容类型。我还仔细检查了它在标题中是否正确。我从 Postman 尝试了几种不同的内容类型,但并没有改变我的结果。我在帖子中添加了一些更详细的日志记录。
-
您是否能够以标准方式在本地运行您的项目并让它处理 POST 请求?如果是这样,那可能意味着这个无服务器包在收到请求和代理到您的应用程序之间的某个地方缺少一些配置。
-
@fram 很好的建议。我能够以标准方式在本地运行它。我会将 StreamLambdaHandler 的代码添加到我的帖子中,以防包含缺少的线索。
标签: java spring spring-boot aws-lambda