1

I have the following REST-style Spring MVC controller that accepts a skillId as a form parameter:

@RequestMapping(value = "{employeeId}/addSkill", method = RequestMethod.PUT)
public void updateEmployeeSkills(@PathVariable Integer employeeId, @RequestParam("skillId") Integer skillId) throws NoSuchRequestHandlingMethodException {
    Employee employee = employeeDao.findEmployee(employeeId);

    if (employee == null) {
        throw new NoSuchRequestHandlingMethodException("No employee #" + employeeId, this.getClass());
    }

    Skill skill = skillDao.findSkill(skillId);
    employee.getSkills().add(skill);

    employeeDao.updateEmployee(employee);
}

And I have the client jQuery AJAX code as follows:

$("#add-skills-modal-form").submit(function() {
    var $form = $(this);
    // .serialize() to send the form input name-value pairs as params.
    $.ajax({
        // url can be obtained via the form action attribute passed to the JSP.
        url: $form.attr("action"),
        data: $form.serialize(),
        type: "POST",
        statusCode: {
            404: function() {
                alert("Employee not found");
            },
            500: function() {
                alert("Failed to update Employee skills");
            }
        },
        success: function() {
            $('#add-skills-modal').modal('hide');
        }
    });
    return false;
});

Now, there is no problem with the update into the database. The AJAX call works perfectly, however, the 404 error status code appears after the AJAX call successfully completes. So basically, an incorrect 404 error occurs, even though the AJAX call works perfectly. Also, the code in the success part of the AJAX call does not get executed which I am finding very peculiar.

Can anyone help me to fix this issue and understand why a 404 error is appearing when it shouldn't.

2
  • The ajax request is a POST and the request mapping is listening for a PUT. Commented Mar 17, 2014 at 15:09
  • Yes, I know. jQuery converts the PUT into a POST. Commented Mar 17, 2014 at 15:46

2 Answers 2

2

Could you try this?

 @RequestMapping(value = "{employeeId}/addSkill", method = RequestMethod.PUT)
    public @ResponseBody String updateEmployeeSkills(@PathVariable Integer employeeId, @RequestParam("skillId") Integer skillId) throws NoSuchRequestHandlingMethodException {
        Employee employee = employeeDao.findEmployee(employeeId);

        if (employee == null) {
            throw new NoSuchRequestHandlingMethodException("No employee #" + employeeId, this.getClass());
        }

        Skill skill = skillDao.findSkill(skillId);
        employee.getSkills().add(skill);

        employeeDao.updateEmployee(employee);
        return "OK";


    }
Sign up to request clarification or add additional context in comments.

Comments

2

Got it. Need to add @ResponseStatus(HttpStatus.NO_CONTENT) to the Spring MVC controller method, e.g.:

@RequestMapping(value = "{employeeId}/addSkill", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.NO_CONTENT) // THIS WAS MISSING
public String updateEmployeeSkills(@PathVariable Integer employeeId, @RequestParam("skillId") Integer skillId) throws NoSuchRequestHandlingMethodException {
    Employee employee = employeeDao.findEmployee(employeeId);

    if (employee == null) {
        throw new NoSuchRequestHandlingMethodException("No employee #" + employeeId, this.getClass());
    }

    Skill skill = skillDao.findSkill(skillId);
    employee.getSkills().add(skill);

    employeeDao.updateEmployee(employee);

    return "redirect:" + EmployeeModelFactory.generateEmployeeUri(employeeId);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.