Skip to content

Using RouterFunctions.route() results in exception #3265

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

Closed
bsgrd opened this issue Feb 15, 2024 · 9 comments
Closed

Using RouterFunctions.route() results in exception #3265

bsgrd opened this issue Feb 15, 2024 · 9 comments
Labels
Milestone

Comments

@bsgrd
Copy link

bsgrd commented Feb 15, 2024

Describe the bug
When I try to read or modify the request body in spring-cloud-gateway-mvc, the body is removed from the request and this exception is thrown:

java.lang.NullPointerException: Cannot invoke "java.util.Map.put(Object, Object)" because the return value of "org.springframework.cloud.gateway.server.mvc.common.MvcUtils.getGatewayAttributes(org.springframework.web.servlet.function.ServerRequest)" is null

I'm using Spring Boot 3.2.2 and cloud 2023.0.0

Sample
I have tried using the BeforeFilterFunctions.modifyRequestBody:
.before(modifyRequestBody(String.class, String.class, MediaType.APPLICATION_JSON_VALUE, (serverRequest, s) -> s))

.. and also a regular filter like this:

.filter((request, next) -> {
	byte[] bytes = StreamUtils.copyToByteArray(request.servletRequest().getInputStream());
	String requestBody = new String(bytes, StandardCharsets.UTF_8);
	LOGGER.info(requestBody);
	return next.handle(request);
})

I have used the example from the documentation: ModifyRequestBody Filter

@NiiazKhan
Copy link

@bsgrd try pass requestBody.
next.handle(requestBody);

@spencergibb spencergibb changed the title Reques body is removed from request when accessed (spring-cloud-gateway-mvc) Request body is removed from request when accessed (spring-cloud-gateway-mvc) Mar 11, 2024
@spencergibb
Copy link
Member

I'm unable to reproduce this using this initializr setup

If you'd like us to spend some time investigating, please take the time to provide a complete, minimal, verifiable sample (something that we can unzip attached to this issue or git clone, build, and deploy) that reproduces the problem.

@bsgrd
Copy link
Author

bsgrd commented Mar 12, 2024

Hi @spencergibb
I created a minimal repo with my setup here boot 3.2.2 and cloud 2023.0.0 here gateway
I'm gonna test with your initializer setup.

@spencergibb
Copy link
Member

I still can't reproduce with your repository. How are you making the http request?

@bsgrd
Copy link
Author

bsgrd commented Mar 12, 2024

@spencergibb weird. I'm doing a simple POST request with postman and a random json body.
I also just tried a curl request with same result:
curl -H 'Content-Type: application/json' -d '{ "title":"foo","body":"bar", "id": 1}' -X POST http://localhost:8080

@bsgrd
Copy link
Author

bsgrd commented Mar 12, 2024

BTW. I should mention that I'm using Java 21. Don't know if that makes a difference. Though i get the same error with java 17.

@spencergibb
Copy link
Member

I see, the path of the uri is not used. See #3292 and #3293

https://github.com/bsgrd/gateway/pull/1

@bsgrd
Copy link
Author

bsgrd commented Mar 13, 2024

@spencergibb I'm still getting the same error with your fix.
I have added a screenshot and the stacktrace.

image

stacktrace.txt

@spencergibb
Copy link
Member

Using GatewayRouterFunctions.route() was the difference. PR updated.

@spencergibb spencergibb changed the title Request body is removed from request when accessed (spring-cloud-gateway-mvc) Using RouterFunctions.route() results in exception Mar 13, 2024
@spencergibb spencergibb added this to the 4.1.2 milestone Mar 13, 2024
@github-project-automation github-project-automation bot moved this to Done in 2023.0.1 Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Status: Done
Development

No branches or pull requests

4 participants