我正在尝试使用Spring Boot容器创建Rest API。我使用了文档中提到的示例代码。我面临的问题是未设置@RequestParam。我使用SAM本地进行测试。我不确定自己在做什么错。
lamda处理程序
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 {
handler.proxyStream(inputStream, outputStream, context);
}
}
应用代码:
@SpringBootApplication
@Import({ PingController.class, JobConfigController.class})
public class Application extends SpringBootServletInitializer {
/*
* Create required HandlerMapping, to avoid several default HandlerMapping instances being created
*/
@Bean
public HandlerMapping handlerMapping() {
return new RequestMappingHandlerMapping();
}
/*
* Create required HandlerAdapter, to avoid several default HandlerAdapter instances being created
*/
@Bean
public HandlerAdapter handlerAdapter() {
return new RequestMappingHandlerAdapter();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
API端点:
@RestController
@EnableWebMvc
public class JobConfigController {
@RequestMapping(
path = "/jobs",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public JSONObject jobs(@RequestParam("app_server_name") String appServerName,
@RequestParam("ms_name") String msName,
@RequestParam("shards") JSONArray shards) {
JSONObject obj = new JSONObject();
// some logic
return obj;
}
}
{
"timestamp": 1551929760142,
"status": 400,
"error": "Bad Request",
"message": "Required String parameter 'app_server_name' is not present",
"path": "/jobs"
}
当我直接在handleRequest方法中记录InputStream时,我可以看到查询参数。
{"httpMethod": "GET", "body": null, "resource": "/{proxy+}", "requestContext": {"resourceId": "123456", "apiId": "1234567890", "resourcePath": "/{proxy+}", "httpMethod": "GET", "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", "accountId": "123456789012", "stage": "prod", "identity": {"apiKey": null, "userArn": null, "cognitoAuthenticationType": null, "caller": null, "userAgent": "Custom User Agent String", "user": null, "cognitoIdentityPoolId": null, "cognitoAuthenticationProvider": null, "sourceIp": "127.0.0.1", "accountId": null}, "extendedRequestId": null, "path": "/{proxy+}"}, "queryStringParameters": {"app_server_name": "cb-staging-app-v9", "ms_name": "cb-app", "shards": "[\"general_test\", \"general_live\"]"}, "headers": {"Host": "127.0.0.1:3000", "Connection": "keep-alive", "Cache-Control": "no-cache", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36", "Postman-Token": "049a9a3a-5411-9c44-382c-6af53279879f", "Accept": "*/*", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "en-US,en;q=0.9,da;q=0.8,fr;q=0.7", "X-Forwarded-Proto": "http", "X-Forwarded-Port": "3000"}, "pathParameters": {"proxy": "jobs"}, "stageVariables": null, "path": "/jobs", "isBase64Encoded": false}
有人可以帮忙吗?我最近4-5天都在挣扎。