Skip to content

Using RouterFunctions.route() results in exception #3265

@bsgrd

Description

@bsgrd

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions