Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

java - Spring Controller replacing + character in parameter with whitespace

I have a controller that takes 2 parameters -

@PostMapping(value = "/myEndPoint")
public ResponseEntity<StreamingResponseBody> process(final InputStream data,
                                                               @RequestParam final String param1,
                                                               @RequestParam final String base64Param)  {
    return ok().body(doSomething(base64Param.getBytes(), param1, data));
}

The base64Param String parameter will contain + characters, but of course the Spring controller replaces these with spaces.

Do you know how I can get around this and tell the controller to leave the + characters alone?

UPDATE - I tried

UriComponentsBuilder builder = fromUriString(format("http://localhost:%s/%s", serverPort, "/myEndPoint"))
            .queryParam("param1", param1)
            .queryParam("base64Param", Base64.getUrlEncoder().encodeToString(base64ParamValueWithPluses.getBytes()));

but I'm having to decode the base64Param parameter in my controller like this -

String decodedBaseParam64 = Base64.getUrlDecoder().decode(encodeBaseParam64);

Is there a way to encode this so Spring will decode it automatically with the + characters preserved?

question from:https://stackoverflow.com/questions/65831140/spring-controller-replacing-character-in-parameter-with-whitespace

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
URI uri = fromUriString(format("http://localhost:%s/%s", serverPort, "/myEndPoint"))
            .queryParam("param1", param1)
            .queryParam("base64Param", "{base64Param}")
            .build(base64Param);

source - https://github.com/spring-projects/spring-framework/issues/21577


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...