Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to bind requestParameter to @ModelAttribute form in serverless ~ Spring boot2 #340

Closed
1tkd opened this issue Apr 23, 2020 · 6 comments
Assignees
Labels
Milestone

Comments

@1tkd
Copy link

1tkd commented Apr 23, 2020

  • Framework version: aws-serverless-java-container-springboot2 1.4
  • Implementations: Spring Boot2 (2.1.8)

Scenario

I send a POST request to aws lambda via api gateway.

Expected behavior

In the RequestMapping method,
the request parameter was mapped to form.

Actual behavior

In the RequestMapping method,
i got null from form object.

Steps to reproduce

  1. maven build
    test project

  2. create lambda function
    runtime:java11
    handler:contract.StreamLambdaHandler

  3. create api gateway
    api type:rest
    /{PROXY+}/ANY
    integration type:lambda function

  4. deloy api gateway

  5. access via web browser
    (http://~/(stage)/start)

  6. input some string to v1 input field

  7. press submit

  8. check v2 input filed

Full log output

START RequestId: f5fb028b-4ad5-42d3-b9ef-d6a37e90c0a6 Version: $LATEST
03:37:23.916 [main] INFO com.amazonaws.serverless.proxy.internal.LambdaContainerHandler - Starting Lambda Container Handler
.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::        (v2.1.8.RELEASE)
2020-04-23 03:37:26.084  INFO 8 --- [           main] lambdainternal.AWSLambda                 : Starting AWSLambda on 169.254.142.165 with PID 8 (/var/runtime/lib/aws-lambda-java-runtime-0.2.0.jar started by sbx_user1051 in /var/task)
2020-04-23 03:37:26.085  INFO 8 --- [           main] lambdainternal.AWSLambda                 : No active profile set, falling back to default profiles: default
2020-04-23 03:37:28.354  INFO 8 --- [           main] c.a.s.p.i.servlet.AwsServletContext      : Initializing Spring embedded WebApplicationContext
2020-04-23 03:37:28.354  INFO 8 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2211 ms
2020-04-23 03:37:28.773  WARN 8 --- [           main] org.thymeleaf.templatemode.TemplateMode  : [THYMELEAF][main] Template Mode 'HTML5' is deprecated. Using Template Mode 'HTML' instead.
2020-04-23 03:37:30.275  INFO 8 --- [           main] lambdainternal.AWSLambda                 : Started AWSLambda in 5.583 seconds (JVM running for 7.226)
2020-04-23 03:37:30.358  INFO 8 --- [           main] c.a.s.p.i.servlet.AwsServletContext      : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-04-23 03:37:30.358  INFO 8 --- [           main] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-04-23 03:37:30.375  INFO 8 --- [           main] o.s.web.servlet.DispatcherServlet        : Completed initialization in 17 ms
2020-04-23 03:37:31.737  INFO 8 --- [           main] c.a.s.p.internal.LambdaContainerHandler  : 211.9.58.241 - [23/04/2020:03:37:22Z] "POST /write HTTP/1.1" 200 358 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36" combined
END RequestId: f5fb028b-4ad5-42d3-b9ef-d6a37e90c0a6
REPORT RequestId: f5fb028b-4ad5-42d3-b9ef-d6a37e90c0a6	Duration: 1438.57 ms	Billed Duration: 1500 ms	Memory Size: 1536 MB	Max Memory Used: 219 MB	Init Duration: 7281.49 ms	
START RequestId: 950d7ed5-2d17-46be-8069-d0ac0afedfb0 Version: $LATEST
2020-04-23 03:37:41.312  INFO 8 --- [           main] c.a.s.p.internal.LambdaContainerHandler  : 211.9.58.241 - [23/04/2020:03:37:41Z] "POST /write HTTP/1.1" 200 355 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36" combined
END RequestId: 950d7ed5-2d17-46be-8069-d0ac0afedfb0
REPORT RequestId: 950d7ed5-2d17-46be-8069-d0ac0afedfb0	Duration: 8.31 ms	Billed Duration: 100 ms	Memory Size: 1536 MB	Max Memory Used: 219 MB	
@sapessi
Copy link
Collaborator

sapessi commented Apr 23, 2020

Hey @1tkd, can you share some sample code to help us replicate?

@1tkd
Copy link
Author

1tkd commented Apr 23, 2020

Thanks for helping.
All codes is here

Controller

@Controller
@EnableWebMvc
public class HelloController {
	@GetMapping(value = "/start", produces="text/html;charset=UTF-8")
	public String getStart(@ModelAttribute HelloForm helloForm) {

		return "startApp";
	}

	@RequestMapping(value = "/write", method = RequestMethod.POST, produces="text/plain;charset=UTF-8")
	public String show(@ModelAttribute HelloForm helloForm, HttpServletRequest req) {
		helloForm.setVal2("form:"+(helloForm.getVal1()==null?"isNull":helloForm.getVal1()) + " req:"+req.getParameter("val1"));
		
		return "startApp";
	}
}

Form

@Data
public class HelloForm implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	String val1;
	String val2;
	
}

Template

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />

<title>Thymeleaf</title>
</head>
<body>
  <form th:action="@{write}" method="post" th:object="${helloForm}">
  v1:  <input type="text" th:field="*{val1}" ><br>
  v2:  <input type="text" th:field="*{val2}" ><br>
  <input type="submit" value="submit">
  </form>
</body>
</html>

@fabricemarchal
Copy link

Any workaround for this issue? Got the same problem when posting application/x-www-form-urlencoded on ApiGateway :
@ModeAttribute is null.

@deki
Copy link
Collaborator

deki commented Dec 30, 2021

Sorry for the late response.

You need to define a mapping for application/x-www-form-urlencoded on ApiGateway.

This forum post https://forums.aws.amazon.com/thread.jspa?messageID=663593&tstart=0#663593 describe a similar configuration. See also https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#input-variable-reference in the documentation.

@1tkd
Copy link
Author

1tkd commented Dec 30, 2021

Thanks for the response.
Am I correct in my understanding of the following?

In the case of aws-serverless-java-container-springboot, I need to convert the request to json format, as shown in the link, in order to set it to ModelAttribute.

As a supplementary note, the data in application/x-www-form-urlencoded is available from HttpServletRequest.

@deki
Copy link
Collaborator

deki commented Jan 3, 2022

@1tkd please ignore my previous commit, this is only valid if you are not using proxy integration. Sorry, I should have initially read your description more carefully.

I deployed your sample after adding the following properties to application.yml:

logging.level.org.springframework.web: TRACE
spring.mvc.log-request-details: true

Comparing the output of the Lambda execution in CloudWatch
o.s.web.servlet.DispatcherServlet : POST "/write", parameters={val1:[test]}

with local execution on Tomcat
o.s.web.servlet.DispatcherServlet : POST "/write", parameters={val1:[test], val2:[]}

indicates that empty parameters are missing. Will add a testcase and proposed fix for that.

@deki deki self-assigned this Jan 3, 2022
@deki deki added the bug label Jan 3, 2022
@deki deki added this to the Release 1.7 milestone Jan 3, 2022
deki added a commit to deki/aws-serverless-java-container that referenced this issue Jan 3, 2022
deki added a commit to deki/aws-serverless-java-container that referenced this issue Jan 3, 2022
@deki deki mentioned this issue Jan 3, 2022
2 tasks
deki added a commit to deki/aws-serverless-java-container that referenced this issue Jan 7, 2022
deki added a commit to deki/aws-serverless-java-container that referenced this issue Jan 7, 2022
@deki deki closed this as completed Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants