简体   繁体   中英

Unsupported Media Type with Spring Boot and Serverless

I am building application with Spring Boot (Serverless project) to deploy in AWS Lambda. I have a method that consumes application/x-www-form-urlencoded . When I call method in localhost 415 appear:

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

I attach method code and yaml configuration, I think that problem is Serverless configuration, because this method works in other project (NON-Serverless).

Method :

 @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

Thanks for your help!

The problem is with mediatype application/x-www-form-urlencoded, Spring Boot doesn't understand this with RequestBody. So, if you want to use, please remove the @RequestBody annotation from method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM