Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting org.springframework.web.bind.MissingServletRequestParameterException

I am using spring annotations I have written one method

public ModelAndView showTestPage(@RequestParam("firstInstanceId") String id1, 
  @RequestParam("secondInstanceId") String id2, HttpSession  session) {

  ModelAndView mv = new ModelAndView("showCompareItemList");
  mv.addObject("pageTitle", "showCompareItemList");
  mv.addObject("firstInstanceId", id1);
  mv.addObject("secondInstanceId", id2);

  return mv;
 }

when there both values of id1 and id2 are present it works fine but when there is only one value i get exception org.springframework.web.bind.MissingServletRequestParameterException: Required java.lang.String parameter 'secondInstanceId' is not present I tried resolve this problem by checking null but still i am getting this exception can anybody tell me what should I do to avoid this excpetion?

like image 746
Vipul Avatar asked Sep 02 '25 16:09

Vipul


1 Answers

If request parameter may be missed, mark it with required = false:

public ModelAndView showTestPage(@RequestParam("firstInstanceId") String id1,    
    @RequestParam(value = "secondInstanceId", required = false) String id2,
    HttpSession session) {
    ...
}
like image 93
axtavt Avatar answered Sep 04 '25 07:09

axtavt