Argamidon
Argamidon

Reputation: 335

I got 404 error after sending POST method from ajax (@ResponseStatus & ResponseEntity)

I ma using Spring MVC and trying to use jQuery. I have this on my web page:

$(document).ready(function () {
    var entity = {mag: "status_key", paper: "View10"};
    $("#btn").click(function () {
        $.ajax({
            url: "ajaxJsonPost",
            type: 'post',
            dataType: 'json',
            data: JSON.stringify(entity),
            contentType: 'application/json',
        });
    });
});

Spring server has this:

@RequestMapping(value = "ajaxJsonPost", method = RequestMethod.POST)
public void postJson(@RequestBody Entity en) throws IOException {
    System.out.println("writing entity: " + en.toString());
}

OK, Entity cames to server. BUT browser console prints 404 not found. I know that my POST request needs any response. In the Internet I've found solution which recommends me to return ResponseEntity object, OR use annotation @ResponseStatus. They both return HttpStatus well, but I don't know in which cases I should use them. What is the best way?

Upvotes: 1

Views: 2113

Answers (1)

Nielarshi
Nielarshi

Reputation: 1136

@Controller
@RequestMapping("/apipath")
public class SomeController {
@RequestMapping(value = "/ajaxJsonPost", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String postJson(@RequestBody final Entity en) {
        System.out.println(en.toString());

        //assuming you have a class "EntityService" and 
        //it has a method postData 
        //which takes Entity object as parameter and pushes into database.
        EntityService.postData(en);

        System.out.println("added");
        return "success";
    }
}

Entity object on the Server side

@JsonAutoDetect
public class Entity {

    private String mag;
    private String paper;

    public String getMag() {
        return mag;
    }

    public void setMag(final String mag) {
        this.mag = mag;
    }

    public String getPaper() {
        return paper;
    }
    public void setPaper(final String paper)
        this.paper = paper;
    }
}

ajax

$(document).ready(function () {
    var entity = {mag: "status_key", paper: "View10"};
    $("#btn").click(function () {
        $.ajax({
            url: "/apipath/ajaxJsonPost",
            type: 'post',
            dataType: 'json',
            data: JSON.stringify(entity),
            contentType: 'application/json',
            success : function(response) {    
                  alert(response);
            },
            error : function() {
                  alert('error');
            }
        });
    });
});

And as far as why and when to use @ResponseStatus and @ResponseEntity, there is already a short and simple answer here by @Sotirios Delimanolis. When use @ResponseEntity .

It says :

ResponseEntity is meant to represent the entire HTTP response. You can control anything that goes into it: status code, headers, and body.

@ResponseBody is a marker for the HTTP response body and @ResponseStatus declares the status code of the HTTP response.

@ResponseStatus isn't very flexible. It marks the entire method so you have to be sure that your handler method will always behave the same way. And you still can't set the headers. You'd need the HttpServletResponse or a HttpHeaders parameter.

Basically, ResponseEntity lets you do more.

Upvotes: 1

Related Questions