简体   繁体   中英

Error “400 Bad Request” The request sent by the client was syntactically incorrect

I am new to jQuery and trying to call Web-Service using Ajax. It seems fairly simple code but somehow not able to understand the reason for its failure. I tried all possible ways (that I could think of) but somehow not getting it to work.

Jsp code:

function tmpData() {
  var dataObject = JSON.stringify({
    'empfname': "First Name",
    'emplname': "Last Name"
});

alert("dataObject=" + dataObject);

$.ajax({
    url:"http://localhost:8080/OnlineStore/kmsg/grocery/tmpinfo",
    type:"POST",
    contentType: 'application/json',
    data: dataObject,
    done: setData,
    fail: displayError()
 });
}; // end of function

Controller:

@RestController
@RequestMapping("/kmsg/grocery")
public class TmpSvcImpl implements TmpSvcInt {
@Override
@RequestMapping(value = "/tmpinfo", method = RequestMethod.POST, headers = "Accept=application/json")
public @ResponseBody Map<String, Object> setData(@RequestBody final Emp employee1) throws Exception {

    System.out.println("employee1=" + employee1);
    String fname = employee1.getEmpfname();
    String lname = employee1.getEmplname();
    System.out.println("fn=" + fname ) ;
    System.out.println("ln=" + lname ) ;

    return null;
  }
}

Model class:

public class Emp implements Serializable {

private static final long serialVersionUID = 1L;
String empfname;
String emplname;

public String getEmpfname() {
    return empfname;
}
public void setEmpfname(String empfname) {
    this.empfname = empfname;
}
public String getEmplname() {
    return emplname;
}
public void setEmplname(String emplname) {
    this.emplname = emplname;
}

public Emp(String fn, String ln){
    this.empfname = fn ;
    this.emplname = ln ;
}

@Override
public String toString() {
    return "Emp {empfname=" + empfname + ", emplname=" + emplname + "}" ;
  }
}

In the block you've described:

@RestController
@RequestMapping("/kmsg/grocery")
public class TmpSvcImpl implements TmpSvcInt {
    @Override
    @RequestMapping(
             value = "/tmpinfo", 
             method = RequestMethod.POST, 
             headers = "Accept=application/json")
    public @ResponseBody Map<String, Object> setData(@RequestBody final Emp employee1) throws Exception {
        System.out.println("employee1=" + employee1);
        String fname = employee1.getEmpfname();
        String lname = employee1.getEmplname();
        System.out.println("fn=" + fname ) ;
        System.out.println("ln=" + lname ) ;
        return null;
    }
}

Specifying the 'Accept' header for this usage case is not really correct. If you want this method to respond only when the content-type is 'application/json' then you will have to add a consumes attribute of 'application/json'. As a comment pointed out this is not strictly necessary. Adding a produces attribute of 'application/json' will ensure that Spring will attempt to marshal the returned content as a JSON structure.

@RestController
@RequestMapping("/kmsg/grocery")
public class TmpSvcImpl implements TmpSvcInt {
    @Override
    @RequestMapping(
             value = "/tmpinfo", 
             method = RequestMethod.POST, 
             consumes = "application/json",
             produces = "application/json")
    public @ResponseBody Map<String, Object> setData(@RequestBody final Emp employee1) throws Exception {
        System.out.println("employee1=" + employee1);
        String fname = employee1.getEmpfname();
        String lname = employee1.getEmplname();
        System.out.println("fn=" + fname ) ;
        System.out.println("ln=" + lname ) ;
        return null;
    }
}

Add default constructor to your Emp class so that Jackson is able to create an instance of it, as follows:

public class Emp implements Serializable {

    private static final long serialVersionUID = 1L;
    public String empfname;
    public String emplname;

    public String getEmpfname() {
        return empfname;
    }

    public void setEmpfname(String empfname) {
        this.empfname = empfname;
    }

    public String getEmplname() {
        return emplname;
    }

    public void setEmplname(String emplname) {
        this.emplname = emplname;
    }

    public Emp(String fn, String ln) {
        this.empfname = fn;
        this.emplname = ln;
    }

    /**
     * default constructor
     */
    public Emp() {

    }

    @Override
    public String toString() {
        return "Emp {empfname=" + empfname + ", emplname=" + emplname + "}";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM