How to Have Empty Requestparam Values Use the Defaultvalue

Is it possible to have empty RequestParam values use the defaultValue?

You could change the @RequestParam type to an Integer and make it not required. This would allow your request to succeed, but it would then be null. You could explicitly set it to your default value in the controller method:

@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public void test(@RequestParam(value = "i", required=false) Integer i) {
if(i == null) {
i = 10;
}
// ...
}

I have removed the defaultValue from the example above, but you may want to include it if you expect to receive requests where it isn't set at all:

http://example.com/test

How to set default value of array to empty with @RequestParam

Just provide an empty string as a default value, no need for anything like [] etc.

@RequestMapping(value = "/stuff, method = GET)
public StuffDTO getStuff(
@RequestParam(value = "stuffIds", defaultValue = "") List<Integer> ids) {

How to give default value as integer in @RequestParam()

Try with "" around integer to make it string as the defaultValue is implemented as String.

@RequestMapping("/returnVeriable")
public int getVeriable(@RequestParam(required=true,defaultValue="1") Integer veri){
return veri;
}

refer : https://jira.spring.io/browse/SPR-5915

Assign null value for default value

Spring's @RequestParam annotation supports:

  • required
  • defaultValue

So, a mapping like this ...

@GetMapping(value = "/{first}")
public ResponseEntity<String> doSomething(@PathVariable int first, @RequestParam(required = false) String foo) {

// ...
}

... defines a request param named foo which is optional and for which null (because null is the unitialised state for a String object) will be provided by Spring if the caller does not pass this param.

Empty immutable collections as defaultValue in @RequestParam

I would start from something like this:
in your config file for the DispatcherServlet you need to register your own ArgumentResolver as you mentioned:

<annotation-driven>
<argument-resolvers>
<beans:bean class="com.foo.bar.CustomListWebArgumentResolver" />
</argument-resolvers>
</annotation-driven>

As I mentioned in my comment, I couldn't see initially a way of dealing with the opposite (normal) way of dealing with a parameter. The solution below doesn't look too pretty in my opinion, but I believe it might work:

public class CustomListWebArgumentResolver implements HandlerMethodArgumentResolver {

@Autowired
private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

@Override
public Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest;
HttpServletRequest request = servletWebRequest.getRequest();

String value = request.getParameter(parameter.getParameterName());
if (value == null || value.isEmpty()) {
return Collections.EMPTY_LIST;
}

RequestParamMethodArgumentResolver defaultResolver = null;
for (Iterator iterator = requestMappingHandlerAdapter.getArgumentResolvers().iterator(); iterator.hasNext();) {
HandlerMethodArgumentResolver type = (HandlerMethodArgumentResolver) iterator.next();
if (type instanceof RequestParamMethodArgumentResolver) {
defaultResolver = (RequestParamMethodArgumentResolver) type;
}
}

if (defaultResolver != null) {
return defaultResolver.resolveArgument(parameter, mavContainer, servletWebRequest, binderFactory);
}

return null;
}

@Override
public boolean supportsParameter(MethodParameter parameter) {
return List.class.equals(parameter.getParameterType());
}
}

Basically, in your custom resolver you get a hold of RequestMappingHandlerAdapter which has a reference to all of the default argument resolvers defined by Spring. The one you are looking for is RequestParamMethodArgumentResolver. Once you get a hold of it, in case the value of the parameter is not null and not empty, you pass the job to the default argument resolver.

Above, in supportsParameter method I assumed a generic List is being handled as a RequestParam:

public String home(@RequestParam(defaultValue = "") List myList, ...) {
...
}

Also, I think you would need a custom configuration to make your custom resolver be considered before the default resolvers:

@Configuration
public class MyCustomWebConfiguration {
private @Inject RequestMappingHandlerAdapter adapter;

@PostConstruct
public void prioritizeCustomArgumentMethodHandlers () {
List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList(adapter.getArgumentResolvers ());
List<HandlerMethodArgumentResolver> customResolvers = adapter.getCustomArgumentResolvers ();

argumentResolvers.removeAll(customResolvers);
argumentResolvers.addAll(0, customResolvers);
adapter.setArgumentResolvers(argumentResolvers);
}
}

Spring Boot non-required requestParams default to null instead of empty string

You could use the defaultValue parameter as in @RequestParam(value = "status", required = false, defaultValue = "") if your statut was a String.

But from your code it looks like it's an enum (EStatus) and with enums defaultValue has to contain a constant expression that could be passed to Enum.valueOf(). If that's the case even if you specify defaultValue = "" the value will still be null. (Obviously it would work if you had a default enum value like "ALL" and used defaultValue = "ALL").

Otherwise you'll have to handle the null value manually either in the controller or in your repository method.



Related Topics



Leave a reply



Submit