gpt4 book ai didi

java - Spring Boot 和 Serverless 不支持的媒体类型

转载 作者:行者123 更新时间:2023-12-02 00:24:09 26 4
gpt4 key购买 nike

我正在使用 Spring Boot(无服务器项目)构建应用程序以部署在 AWS Lambda 中。我有一个消耗 application/x-www-form-urlencoded 的方法。当我在 localhost 415 中调用方法时出现:

Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

我附上了方法代码和yaml配置,我认为问题出在Serverless配置上,因为这个方法适用于其他项目(非Serverless)。

方法:

 @PostMapping(name = "/createpayment", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String createPayment(@RequestBody Payload payload) throws StripeException, IOException, MessagingException {
...
}

Yaml:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS Serverless Sansaru Payments - com.sansarushop.payments::sansaru-payments
Globals:
Api:
EndpointConfiguration: REGIONAL


Resources:
SansaruPaymentsFunction:
Type: AWS::Serverless::Function
Properties:
Handler: com.example.payments.StreamLambdaHandler::handleRequest
Runtime: java8
CodeUri: target/example-payments-1.0-SNAPSHOT-lambda-package.zip
MemorySize: 512
Policies: AWSLambdaBasicExecutionRole
Timeout: 30
Events:
GetResource:
Type: Api
Properties:
Path: /{proxy+}
Method: any
Environment:
Variables:
SPRING_PROFILES_ACTIVE: 'prod'

Outputs:
SansaruPaymentsApi:
Description: URL for application
Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/ping'
Export:
Name: ExamplePaymentsApi

感谢您的帮助!

最佳答案

问题出在 mediatype application/x-www-form-urlencoded 上,Spring Boot 无法理解 RequestBody 的这一点。因此,如果您想使用 ,请从方法中删除 @RequestBody 注解。

@PostMapping(name = "/createpayment", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String createPayment(@RequestParam Map<String, String> params) throws StripeException, IOException, MessagingException {
...
}

关于java - Spring Boot 和 Serverless 不支持的媒体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58065930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com