@JsonDeserialize annotation can be applied to @RequestParam parameters in Spring Boot by creating a custom argument resolver.
Here are the steps:
Create a class that implements the HandlerMethodArgumentResolver interface of Spring
Override the supportsParameter() method to check if the parameter is of type @RequestParam and requires @JsonDeserialize annotation
Override the resolveArgument() method to convert the request parameter to the desired object using ObjectMapper and JsonDeserialize
Annotate the controller method parameter with @JsonDeserialize and the desired class
Add the custom argument resolver to the list of argument resolvers
Here is an example:
public class JsonRequestParamArgumentResolver implements HandlerMethodArgumentResolver {
private final ObjectMapper objectMapper;
public JsonRequestParamArgumentResolver(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterAnnotation(RequestParam.class) != null
&& parameter.getParameterAnnotation(JsonDeserialize.class) != null;
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
String requestParamValue = webRequest.getParameter(parameter.getParameterName());
Class<?> targetClass = parameter.getParameterAnnotation(JsonDeserialize.class).as();
return objectMapper.readValue(requestParamValue, targetClass);
}
}
In the controller:
@GetMapping("/example")
public ResponseEntity<ExampleResponse> example(@JsonDeserialize(as = ExampleRequest.class) @RequestParam("request") ExampleRequest request) {
// logic here
}
To add the custom argument resolver to the list of argument resolvers in Spring:
@Configuration
public class WebConfig implements WebMvcConfigurer {
private final ObjectMapper objectMapper;
public WebConfig(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new JsonRequestParamArgumentResolver(objectMapper));
}
}
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-06 08:41:18 +0000
Seen: 8 times
Last updated: Jun 06 '23
Why am I not getting a response when making a URL call in ASP.NET Web API?
How to provide parameters to machine learning model objects?
How can you obtain several parameters using a single GET request?
How can Rails be configured to allow one key and require another using strong parameters?
What is the process of creating global parameters in OpenAPI?
How can I include custom header parameters in request details when using swagger?
What are some queries regarding the simulation of pic_poll_read in kvm?
How to pass parameter correctly from @Url.Action to method?
Explain what the main file is, but do not give any details about the parameters it contains.