I'm using a @RequestParam on a String parameter, gives defaultValue = ""
. When I call the api without this parameter, I suppose to get an empty value of String "" but got null in my case.
Example that creates the bug:
@GetMapping
public String search(@RequestParam(defaultValue = "") String email) {
System.out.println("Email should be an empty string if not provided: " + email);
return email;
}
When I call this api with no parameter, I got "null" in my console rather than "" (empty String value).
Comment From: sbrannen
What do you mean by “call this API”?
How are you invoking the API?
MockMvc, WebTestClient, …?
Comment From: Adamatoma
What do you mean by “call this API”?
How are you invoking the API?
MockMvc, WebTestClient, …?
Hi sbrannen, Thanks for replying! I put this part of code into a controller file in a java web spring-boot project as an restful API. Then use postman to send requeset to invoke this API.
Hope the following code example helpful
@RestController
@RequestMapping(value = "/testurl/url", produces = "application/json")
public class emailController {
@GetMapping
public String search(@RequestParam(defaultValue = "") String email) {
System.out.println("Email should be an empty string if not provided: " + email);
return email;
}
}
Then call this api with postman with GET Http request with url like127.0.0.1:8080/testurl/url
Comment From: rstoyanchev
I was not able to reproduce the issue. Please provide a sample that reproduces the issue.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.