Simcha
Simcha

Reputation: 3400

Spring: The request sent by the client was syntactically incorrect ()

Hi I received next error during the redirect:

The request sent by the client was syntactically incorrect

URL which browser shows is: localhost:8080/Project/menu/main/home/0 and here my classes with redirects first - "from", second "to":

 /*
 * Get all possible values of menu and generate correct url to pages controllers
 * 
 */

@Controller
@SessionAttributes("menu")
public class MainMenuController {


    @ModelAttribute
    public Menu createMenu() {
        return new Menu();
    }

    @RequestMapping(value = "/menu", method = RequestMethod.GET)
    public String mainMenuResolver(@ModelAttribute Menu menu) {
        menu.setMainMenu("first");
        return "forward:/menu/first";
    }

    @RequestMapping(value = "/menu/{mainMenu}", method = RequestMethod.GET)
    public String subMenuResolver(@PathVariable String mainMenu, @ModelAttribute Menu menu) {
        menu.setMainMenu(mainMenu);
        menu.setSubMenu("home");
        return "forward:/menu/first/home";
    }

    @RequestMapping(value = "/menu/{mainMenu}/{subMenu}", method = RequestMethod.GET)
    public String secMenuResolver(@PathVariable String mainMenu, @PathVariable String subMenu, @ModelAttribute Menu menu) {
        menu.setMainMenu(mainMenu);
        menu.setSubMenu(subMenu);
        menu.setSecMenu("0");

        if (menu.getMainMenu().equals("first")){
            return "redirect:/menu/main/"+menu.getSubMenu()+"/"+menu.getSecMenu();
        }

        if (menu.getMainMenu().equals("second")){
            return "redirect:/menu/religion/"+menu.getSubMenu()+"/"+menu.getSecMenu();
        }

        return "redirect:/menu/main/"+menu.getSubMenu()+"/"+menu.getSecMenu();
    }
}

Second class:

@Controller
@SessionAttributes("menu")
public class FirstPageController {

    @ModelAttribute
    public Menu createMenu() {
        return new Menu();
    }

    @RequestMapping(value = "/menu/main/{subMenu}/{secMenu}", method = RequestMethod.GET)
    public ModelAndView menuResolver(@PathVariable String mainMenu, @PathVariable String subMenu,@PathVariable String secMenu, @ModelAttribute("menu") Menu menu) {
        menu.setMainMenu(mainMenu);
        menu.setSubMenu(subMenu);
        menu.setSecMenu(secMenu);       

        if (menu.getSubMenu().equals("home")){
            String title = "Project - Home Page";
            return new ModelAndView("MainPage", "title", title);
        }

        String title = "Project - Home Page";
        return new ModelAndView("MainPage", "title", title);
    }
}

Solved: I solved it, there excess parameter in the method of the second class.

Upvotes: 19

Views: 46782

Answers (4)

user7375303
user7375303

Reputation:

In my case, it was also a problem of conversion, Spring was expecting an Integer however I was entering a String. Try to check what you have passed as parameters to the controller

Upvotes: 0

ike3
ike3

Reputation: 1800

In cases like this it is very useful to have org.springframework.web loggin level set to DEBUG in log4j configuration

<logger name="org.springframework.web">
    <level value="DEBUG" />
    ...
</logger>

E.g. when parameter is missing or cannot be converted to the required type there will be an exception details in the log.

Upvotes: 36

An&#237;bal
An&#237;bal

Reputation: 925

As said ike3, using the detailed log aided a lot to find the solution for me. In my case it was a mismatch between @PathVariable without name specified, and the variable itself.

Something like this:

@RequestMapping("/user/{uname}")
public String doSomething(@PathVariable String username) { ...

Note the difference between "uname" and "username" ! There was an exception internally that wasn't raised and I couldn't see it until I set the log to INFO level.

Upvotes: 0

ivan.mylyanyk
ivan.mylyanyk

Reputation: 2101

In my case the reason of this error was that browser (Chrome, in my particular case) was sending the date from the <input type="date" ... /> to the server in the wrong format so server didn't know how to parse it.

Upvotes: 2

Related Questions