0

If I post json in jquery,and then can get data in springmvc with @RequestBody

$.ajax({
        url:"menu_selectByName",
        method:"post",
        data:JSON.stringify({menuName:"index"}),
        contentType:"application/json",
        success:function(res){
            $scope.content = res;
        }}
)

@RequestMapping(value = "/menu_selectByName") 
@ResponseBody
public Menu selectByName(@RequestBody String json){
    //json = "{"menuName":"index"}"
    System.out.println(json):

    //other operations
}

but if I post key-value pair with x-www-form-urlencoded,then springmvc can not get datas

$.ajax({
        url:"menu_selectByName",
        method:"post",
        data:{menuName:"index"},
        contentType:"application/x-www-form-urlencoded",
        success:function(res){
            $scope.content = res;
        }}
)

@RequestMapping(value = "/menu_selectByName")
@ResponseBody
public Menu selectByName(String menuName){
    //menuName = null
    System.out.println(menuName);
    return  menuService.selectByName(menuName);
}

if i write like this:

@RequestMapping(value = "/menu_selectByName")
@ResponseBody
public Menu selectByName(@RequestParam("menuName") String menuName){
    //menuName = null
    System.out.println(menuName);
    return  menuService.selectByName(menuName);
}

it will report

  HTTP Status 400 – Bad Request

  Type:Status Report

  Message:Required String parameter 'menuName' is not present

  Description:The server cannot or will not process the request due to 
something that is perceived to be a client error (e.g., malformed request 
syntax, invalid request message framing, or deceptive request routing).

where did I make a mistake?

findsky
  • 21
  • 2
  • It's been a while since I did some spring, but I vaguely remember you may have to do something in your parameters like `selectByName(@RequestParam('menuName') String menuName)` so it knows which param to map to that variable. Possibly related https://stackoverflow.com/questions/13715811/requestparam-vs-pathvariable – Taplar Aug 03 '17 at 06:14
  • I had tried to add `@RequestParam("menuName")`, but if I do this,it will report `http status 400 - bad request`,message is `Required String parameter 'menuName' is not present` – findsky Aug 03 '17 at 07:00

0 Answers0