【问题标题】:Unsupported Media Type with Spring Boot and ServerlessSpring Boot 和 Serverless 不支持的媒体类型
【发布时间】:2019-09-23 15:44:16
【问题描述】:

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

内容类型 'application/x-www-form-urlencoded;charset=UTF-8' 不是 支持

我附上了方法代码和yaml配置,我认为问题是无服务器配置,因为这种方法适用于其他项目(非无服务器)。

方法

 @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

感谢您的帮助!

【问题讨论】:

    标签: java amazon-web-services spring-boot aws-lambda yaml


    【解决方案1】:

    问题出在媒体类型 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 { 
             ...
     }
    

    【讨论】:

      猜你喜欢
      • 2017-09-22
      • 2020-06-26
      • 2021-12-21
      • 2017-06-21
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 2022-12-09
      相关资源
      最近更新 更多