Controller method/browser not recognizing @RequestParam

I have a controller method:

@RequestMapping(value = "/editComment/{id}", method = RequestMethod.GET)
public ModelAndView showOfficeComment(@RequestParam(value="id", required = false) Long id) {
        Office office;          
        office = officeServiceImpl.find(id);
        ModelAndView result = new ModelAndView("editComment", "command", office);
        return result;
    }

When I put the address in the browser with an id at the end (...editComment/100), I get the following error message:

java.lang.IllegalArgumentException: id to load is required for loading

I have the following method that works perfectly:

@RequestMapping(value = "/officeForm/{id}", method=RequestMethod.GET) 
public ModelAndView showOfficeForm(@RequestParam(value="id", required = false) Long id) {
        Office office;
        if (id == null) {office = new Office();}
        else {office = officeServiceImpl.find(id);}
        office.setDepartments(new ArrayList<Department>());
        List<Department> departments = departmentServiceImpl.findAll();

        ModelAndView result = new ModelAndView("officeForm", "command", office);
        result.addObject("departments", departments);
        return result;
    }

Can anybody explain what's happening here? Thanks.

Answers


You should be using @PathVariable instead of @RequestParam as described here.


Need Your Help

R changing a variable name for each data set within a list

r list names

I have a list of dataframes sub_lst. Within each dataframe I want to change the name of the variable in column 1. The varaible name is different in all the dataframes so I'm assuming I need to use