Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does @RequestParam in Spring handle Guava's Optional?

@RequestMapping(value = "/contact.html", method = RequestMethod.POST) public final ModelAndView contact(         @RequestParam(value = "name", required = false) Optional<String> name) { 

How does Spring's @RequestMapping handle an Optional from Guava library if the parameter value is not required and nothing is sent?

Will it be:

  • Set to null
  • Set to Optional.absent()

Can Optional.fromNullable(T) be used to accept the request?

like image 994
Anders Avatar asked Nov 27 '13 08:11

Anders


People also ask

How does Spring boot handle optional request parameters?

You can do it in three ways: Set required = false in @RequestParam annotation. Set defaultValue = “any default value” in @RequestParam annotation. Using Optional keyword.

What does @RequestParam do in Spring?

@RequestParam annotation enables spring to extract input data that may be passed as a query, form data, or any arbitrary custom data.

Can RequestParam be optional?

Optional Request ParametersMethod parameters annotated with @RequestParam are required by default.

Can I use @RequestParam annotation for a post request?

I've read in this post that @RequestParam can only be used for get methods, but if I remove @RequestParam and stick with the params argument of the @PostMapping annotation it still doesn't work. I know I can use @RequestBody but I do not want to make a class just for those 4 parameters.


2 Answers

EDIT (October 2015): Spring 4 handles java.util.Optional (from Java 8) out of the box and guarantees that Optional itself is not null, but original question was about Guava's com.google.common.base.Optional which usage as @RequestParam is highly discouraged in this specific case (because it can be null).

ORIGINAL ANSWER (about Guava's Optional):

Don't do that, just use String and let Spring handle null in its way.

Optional<T> is supposed to be used as return value and rarely as a parameter. In this particular case Spring will map missing "name" parameter to null, so even if after implementing custom property editor you'll finish with null check:

@RequestMapping("foo") @ResponseBody public String foo(@RequestParam(required = false) final Optional name) {   return "name: " + (name == null ? "null" : name.get()); } 

which is completely unnecessary (and missuses Optional), because it can be achieved with:

@RequestMapping("foo") @ResponseBody public String foo(@RequestParam(required = false) final String name) {   return "name: " + (name == null ? "null" : name); } 
like image 119
Xaerxess Avatar answered Sep 20 '22 19:09

Xaerxess


I recommend to use the Java 8 version: java.util.Optional. Look at the Oracle documentation in http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html. Also put a name to the variable, specially if your using Spring 3 or higher:.

import java.util.Optional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView;  @Controller public class LoginController {      private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);      @RequestMapping(value = "/login", method = RequestMethod.GET)     public ModelAndView getLoginPage(@RequestParam(name = "error", required = false) Optional<String> errorMsg)     {         //error.ifPresent(LOGGER::debug); //Java 8 with Optional         return new ModelAndView("login", "error", errorMsg);     }  } 

java.util.Optional is very useful for managing optional parametrers, like errors in Spring.

like image 45
EliuX Avatar answered Sep 19 '22 19:09

EliuX