error in spring 3 rest webservice 406 (Not Acceptable)

563 Views Asked by At

My code:

<script type="text/javascript" src="JavaScript/jquery-1.9.1.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("#login").click(function() {
            var name1 = $("#eid").val();
            var pass1 = $("#p").val();
            var datacontent = {
                "empName": name1,
                "empPassword": pass1
            };

            alert(JSON.stringify(datacontent));

            $.ajax({
                type: "POST",
                accept: "application/json",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                url: "/rest_test1/empLogin.htm",
                data: JSON.stringify(datacontent), // Note it is important

                success: function(data) {
                    alert(data.empName + " " + data.empPassword);
                }
                // error: function(data, status, er) {
                //     alert("Data: " + data + "Status: " + status + "Error:" + er);
                // }
            });
        });
    });
</script>

Controller code:

@Controller
public class EmployeeRestController {
    @RequestMapping(value = "/empLogin.htm", method = RequestMethod.POST)
    public @ResponseBody EmployeeVo doRegister(@RequestBody EmployeeVo employeeVo){
        System.out.println(employeeVo.getEmpName() + " " + employeeVo.getEmpPassword());
        return employeeVo;
    }
}

When I run this code values are shown in console but get error:

406 (Not Acceptable)

So below code is not working:

success: function(data) {
    alert(data.empName + " " + data.empPassword);
}

I used jackson-annotations-2.3.0.jar, jackson-core-2.3.4.jar, jackson-databind-2.3.4.jar Jar files.

1

There are 1 best solutions below

0
On

HTTP status code 406 means the server cannot respond in the format mandated by the request's accept header. In other words, your web service isn't configured to generate an application/json response.

Try removing the accept: "application/json" from your AJAX request, or add the content type to your response, like so:

@RequestMapping(value="/empLogin.htm", headers="Content-Type=application/json", method=RequestMethod.POST)