Spring 4 @RequestMapping-使用vs标头吗? [英] Spring 4 @RequestMapping -- consumes vs headers?

查看:113
本文介绍了Spring 4 @RequestMapping-使用vs标头吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用Spring 4构建RESTful Web服务,我不清楚的一件事是@RequestMapping.我已经看到了一些使用headers = "Accept=application/xml"的示例,而其他一些使用了消费(或产生)= "application/xml"的示例.

例如,在我自己的@RestController类中,我具有此功能...

// POST
@RequestMapping(method = RequestMethod.POST, headers = "Accept=application/xml")
public User create(@RequestBody User user) {
    LOG.info("User = " + user.toString());
return userService.create(user);
}

使用headers = "Accept=application/xml"与使用消耗= "application/xml"?甚至使用headers = "content-type=application/xml"有什么区别?

有人可以解释标题和消费/生产之间的区别以及何时使用它们吗?

解决方案

缩短答案
在上面的示例中,使用headers = "Accept=application/xml"produces = "application/xml"都将以相同的方式响应客户端,即以XML表示形式向客户端发送响应.

更大的答案
i.标头
对于RESTful Web服务,客户端(例如您的浏览器)向服务器发送请求(例如GET,POST等),服务器将向后发送响应.这是一个HTTP事务.请求和响应都有HTTP标头字段(标头"),它们定义HTTP事务的操作参数(我将客户端请求的标头称为请求标头",这些与服务器响应响应"的标头不同)标头").

作为浏览器发送到服务器的请求的一部分,有不同的请求标头,其中一些示例包括AcceptConnectionContent-Length等.这些标头中的每一个都有自己的功能(请参阅完整列表)此处的标题: https://en.wikipedia.org/wiki/List_of_HTTP_header_fields ).

使用您的代码示例,如果客户端发出POST请求,Spring将检查请求标头,并且如果找到具有application/xml值的标头Accept,它将把请求映射到create您上面具有的方法(在这种情况下,服务器将向客户端返回XML响应表示形式).

让我修改您提供的代码中的headers元素:

@RequestMapping(method = RequestMethod.POST, headers = "Connection=keep-alive")
public User create(@RequestBody User user) {
 ...
}

注意,headers元素的值现在为Connection=keep-alive.如果客户端发出POST请求,Spring将检查请求标头,如果发现标头值为keep-alive的标头Connection,它将将该客户端请求映射到上面的create方法.

ii.生产和消费
如果对create方法使用了produces="application/xml",这意味着仅当客户端的Accept标头与application/xml匹配时,客户端请求才会映射到create方法.基本上,这是客户在说:嘿,服务器,我希望以XML表示形式接受您的响应,因此以XML形式将您的响应发送给我."实际上,produces="application/xml"也是服务器说的:客户端,我只能以XML表示形式为您生成响应,所以我将向您发送该格式". 链接到Spring文档参考.

如果对create方法使用了consumes="application/xml",这意味着仅当客户端的Content-Type标头与application/xml匹配时,客户端请求才会映射到create方法(Content-Type请求标头描述了表示客户端请求即将到来).从本质上讲,服务器在说:客户端,我只能使用XML表示形式的请求,因此请将该格式发送给我."

摘要
@RequestMapping批注中的headers元素可以采用不同的请求标头(AcceptConnectionCache-Control等),但是produces元素仅与Accept请求标头和consumes元素仅与Content-Type请求标头有关.

I'm learning how to build RESTful web services using Spring 4, and one thing I'm not clear on is in @RequestMapping. I've seen examples where one uses headers = "Accept=application/xml" and other examples using consumes (or produces) = "application/xml".

For instance, in my own @RestController class, I have this function...

// POST
@RequestMapping(method = RequestMethod.POST, headers = "Accept=application/xml")
public User create(@RequestBody User user) {
    LOG.info("User = " + user.toString());
return userService.create(user);
}

What is the difference between using headers = "Accept=application/xml" vs. using consumes = "application/xml"? Or even using headers = "content-type=application/xml"?

Could someone explain the differences between headers and consumes/produces, and when each is used?

解决方案

SHORT ANSWER
In the example you have above, using headers = "Accept=application/xml" or produces = "application/xml" will both respond to the client the same way i.e. send a response to the client with XML representation.

LONGER ANSWER
i. Headers
For RESTful web services, the client (e.g. your browser) sends a request (e.g. GET, POST, etc.) to a server, and the server will send a response back. This is an HTTP Transaction. Both the request and response have HTTP header fields ("headers"), which define the operating parameters of an HTTP transaction (I will refer to the headers for client request as "request headers", and these differ from headers from server response "response headers").

As part of the request your browser sends to server, there are different request headers and some examples include Accept, Connection, Content-Length etc. and each of these headers have their own function (see a full list of headers here: https://en.wikipedia.org/wiki/List_of_HTTP_header_fields).

Using your code example, if a client does a POST request, Spring will check the request header(s) and if it finds a header Accept with a value of application/xml, it will map the request to the create method you have above (and in your case the server will return an XML response representation to the client).

Let me modify the headers element in the code you provided:

@RequestMapping(method = RequestMethod.POST, headers = "Connection=keep-alive")
public User create(@RequestBody User user) {
 ...
}

Notice the headers element now has a value of Connection=keep-alive. If a client does a POST request, Spring will check the request header(s) and if it finds a header Connection with a value of keep-alive, it will map that client request to the create method above.

ii. Produces and Consumes
If you used produces="application/xml" for the create method, this means a client request is only mapped to the create method if the client's Accept header matches application/xml. This essentially is the client saying, "Hey server, I prefer to accept your response in XML representation, so send your response to me in XML". Effectively, the produces="application/xml" is also the server saying, "Hey client, I can only produce responses for you in XML representation, so I will send you that format". Link to Spring documentation reference.

If you used consumes="application/xml" for the create method, this means a client request is only mapped to the create method if the client's Content-Type header matches application/xml (the Content-Type request header describes the representation the client request is coming in). This essentially is the server saying, "Hey client, I can only consume requests in XML representation, so send that format to me".

SUMMARY
The headers element within the @RequestMapping annotation can take different request headers (Accept, Connection, Cache-Control etc.), but the produces element is only concerned with the Accept request header and the consumes element is only concerned with the Content-Type request header.

这篇关于Spring 4 @RequestMapping-使用vs标头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆