【发布时间】:2021-05-31 20:36:33
【问题描述】:
我正在尝试在 React 应用程序中使用 API,但我收到此错误作为响应。
{errorMessage: "An error occurred during JSON parsing", errorType: "java.lang.RuntimeException", stackTrace: Array(0), cause: {…}}
cause:
cause: {errorMessage: "Can not deserialize instance of java.lang.String o…MemoryAsInputStream@727803de; line: 1, column: 1]", errorType: "com.fasterxml.jackson.databind.JsonMappingException", stackTrace: Array(6)}
errorMessage: "com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token↵ at [Source: lambdainternal.util.NativeMemoryAsInputStream@727803de; line: 1, column: 1]"
errorType: "java.io.UncheckedIOException"
这是从 React 发出的 POST 请求。
async handleSubmit(event) {
event.preventDefault();
const data = "{\"type\":\"select\",\"startdate\":\""+this.state.startdate+"\",\"enddate\":\""+this.state.enddate+"\"}";
const url = "<API Gateway endpoint>";
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: "{\"type\":\"select\",\"startdate\":\"2020-12-15 09:59:59\",\"enddate\":\"2021-01-13 21:37:43\"}"
});
const body = await response.json();
this.setState({invoices : body, isLoading:false})
console.log(this.state.invoices);
}
问题很可能是 Java 处理程序方法无法解析请求数据。我不确定如何构造请求正文,以便 Lambda 处理程序接受请求正文作为输入参数。
public static String handleRequest(String input, Context context) {
Connection conn = createConnection();
Statement stmt = null;
JSONObject jsonObject = null;
<JDBC stuff>
return jsonObject.toString();
}
【问题讨论】:
-
您是否使用 LAMBDA 或 LAMBDA_PROXY 与 Api Gateway 集成?
-
我正在使用 LAMBDA 集成
标签: java reactjs lambda aws-lambda aws-api-gateway