How exactly works the @ResponseStatus Spring annotation for RESTful application?

I am studying for the Spring Core certification and I have some doubt about how Spring handle REST request.

I know that with REST the resources are exposed as name and that the action of these resources are the HTTP methods: GET , PUT , POST , DELETE

And I know that the request are handled by the use of @RequestMapping annotations over the method that handle the resource.

From what I have understand both standard web applications and RESTful applications use some codes to communicate with their clients (the RESTful application have an expanded set of codes) that I think represent the status of the request (for example 200 is the request is a successful GET returning content, etcetc).

Now in the documentation show the use of the @ResponseStatus annotation showing this example:

@RequestMapping(value="/orders", method=RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED) // 201
public void createOrder(HttpServletRequest request, HttpServletResponse response) {
    Order order = createOrder(request);
    // determine full URI for newly created Order based on request
    response.addHeader("Location",
    getLocationForChildResource(request, order.getId()));
}

So looking the previous method I know that it handle HttpRequest POST request towards the resource named /orders (using REST the resource is seen as an URL, is correct?).

But what exactly do the annotation:

@ResponseStatus(HttpStatus.CREATED) // 201

I know that the 201 status code means that a nNew resource was created on POST or PUT

and looking on the official documentation I can read:

Marks a method or exception class with the status code and reason that should be returned. The status code is applied to the HTTP response when the handler method is invoked, or whenever said exception is thrown.

So what exactly means? I think that as is done in the previous example it set the 201 status that say that the resource is correctly created by the POST request. If this is correct I have 2 questions:

  • The resource is the /orders URI. So what is created? a file named orders (I think that this assertion is false) or what?

  • Where the 201 status is putted?


  • 201 is an HTTP status code. It indicates that the

    request has been fulfilled and resulted in a new resource being created.

    So if your server is returning such a status code, then the client understands that some (conceptual) resource was created. What that resource is is your responsibility, you're the server.

    A status code is part of the HTTP response status line.

    链接地址: http://www.djcxy.com/p/83632.html

    上一篇: 未找到匹配的C ++模板运算符

    下一篇: RESTful应用程序的@ResponseStatus Spring注释究竟如何工作?