I am experiencing an issue where query parameters are not being recognized by Spring in certain cases. Despite seeing the correct query parameters in the logs, Spring seems to be unable to detect them in specific scenarios, leading to unexpected behavior. I have some edpoint user/settings?ids=names
. I see in Kibana and Sentry that request is user/settings?ids=names
but i have this error MissingServletRequestParameterException ids parameter is not present
.
My Controller:
```@RestController
@RequestMapping("/user/settings")
public class UserSettingsController {
private final UserSettingsService userSettingsService;
public UserSettingsController(UserSettingsService userSettingsService) {
this.userSettingsService = userSettingsService;
}
@GetMapping
public ResponseEntity<Map<String, String>> getUserSettings(@RequestParam List<String> ids,
@AuthenticationPrincipal Integer userId) {
return ids.isEmpty() ? noContent().build() : ok(userSettingsService.getSettings(userId, ids));
}
Also I added some logs to my ControllerAdvice:
@ExceptionHandler(MissingServletRequestParameterException.class)`
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse handleMissingServletRequestParameterException(MissingServletRequestParameterException ex,
HttpServletRequest request,
HttpServletResponse response) {
Map
logger.error("Exception while processing request. Details: {}", details, ex);
return new ErrorResponse(ErrorReason.SC_INTERNAL_SERVER_ERROR);
}
And got this logs:
Exception while processing request. Details: {URL=https://api.com/user/settings?ids=name, Method=GET, Remote address=2600:1700:543c:7c00:159b:eb48:f7f9:4837, Referer=null, Parameters failed=null, Failed parameters=null} ```
And problem is that I cannot reproduce this locally. I created script which spam this endpoint.
How it's possible? Could u describe me is it possible to remove from request.getParameterMap() values? Or maybe this is about thread safety?
Actual Behavior: In some cases, Spring fails to detect the query parameters that are clearly present in the logs. This causes unexpected behavior and may lead to incorrect processing of the requests.
Spring Boot: 2.7.3
Comment From: gusoliveiira
Corret me if I'm wrong, but I think that query param cant be a list as a get in rest comunication. You must pass onde Id per time. I think that is a limitation in the protocol http/rest.
To deal with it, you can do a "post" with request body List
Comment From: Rostyslav26
Corret me if I'm wrong, but I think that query param cant be a list as a get in rest comunication. You must pass onde Id per time. I think that is a limitation in the protocol http/rest. To deal with it, you can do a "post" with request body List
. Its not the best way, but I think that is a limitation of protocol http/rest, not spring.
I don't think it's correct, these queries run successfully, for example, I don't see any problem in a query like /reports/search?types=firtstType,secondType. Just in some cases that are incomprehensible to me, the spring does not see them
Comment From: robbyzhao
I see in Kibana and Sentry that request is user/settings?id=names but i have this error MissingServletRequestParameterException ids parameter is not present
As you described above, seems the params's name is id
but not ids
as you defined in the endpoint when you got the exception.
Comment From: Rostyslav26
I see in Kibana and Sentry that request is user/settings?id=names but i have this error MissingServletRequestParameterException ids parameter is not present
As you described above, seems the params's name is
id
but notids
as you defined in the endpoint when you got the exception.
sorry, its my typo in the question. In my project I have right path. Also, for more info, I found, that I use HandlerInterceptorAdapter for my interceptors, and from dock, its processed async. Maybe this is my problem? I will change this to HandlerInterceptor on 20 Jun for testing. But maybe u have quick answer)
Comment From: robbyzhao
I see in Kibana and Sentry that request is user/settings?id=names but i have this error MissingServletRequestParameterException ids parameter is not present
As you described above, seems the params's name is
id
but notids
as you defined in the endpoint when you got the exception.sorry, its my typo in the question. In my project I have right path. Also, for more info, I found, that I use HandlerInterceptorAdapter for my interceptors, and from dock, its processed async. Maybe this is my problem? I will change this to HandlerInterceptor on 20 Jun for testing. But maybe u have quick answer)
Since I didn't have much knowledge about your code and environments, maybe you are right, just give a try.
Comment From: snicoll
Unfortunately, it's very hard for us to figure it out. You're saying that you can't reproduce this locally so it should be something in the other environment intercepting requests and manipulating them. In the absence of a concrete problem, this issue is not actionable. You may want consider StackOverflow to get more feedback from the community. If it turns out to be a bug, please share a small sample we can run ourselves with instructions to reproduce, and we'll reopen.