@RequestParam не установлены при использовании SpringBootLambdaContainerHandler - PullRequest
0 голосов
/ 07 марта 2019
  • Версия фреймворка: 1.3.1
  • Реализации: 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"
}

Отладка

Когда я регистрирую InputStream непосредственно в методе handleRequest, я вижу параметр запроса.

{"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 дней.

...