简体   繁体   English

在这两种情况下有关Spring MVC如何接收和使用Accept Header的一些信息

[英]Some information about how Spring MVC recive and use the Accept Header in these two case

I am studying on the Spring MVC showcase example dowlodable from the STS dashboard. 我正在研究可从STS仪表板连接的Spring MVC展示示例。

In this time I am studying on the Converters section of this example and I have some question for you. 这次,我正在研究本示例的“转换器”部分,并且对您有一些疑问。

To start, in my view I have the following two links: 首先,在我看来,我有以下两个链接:

<li>
    <a id="writeJsonAccept" class="writeJsonLink" href="<c:url value="/messageconverters/json" />">Write JSON via Accept=application/json</a>
</li>

<li>
    <a id="writeJsonExt" class="writeJsonLink" href="<c:url value="messageconverters/json" />">Write JSON via ".json"</a>
</li>

The first link generate an HTTP Request towards the URL: messageconverters/json 第一个链接生成指向URL的HTTP请求: messageconverters / json

The second link generate an HTTP Request towards the URL: /messageconverters/json.json (differently from the first URL this one end with .json extension 第二个链接生成一个指向URL的HTTP请求: /messageconverters/json.json (与第一个URL不同,该扩展名以.json扩展名结尾

Ok, both these links have class="writeJsonLink" and related to the click event of these links the following JQuery callback function is called: 好的,这两个链接都具有class =“ writeJsonLink”,并且与这些链接的click事件相关,因此将调用以下JQuery回调函数:

$("a.writeJsonLink").click(function() {

    var link = $(this);                      

    $.ajax({ 
        url: this.href,  

        beforeSend: function(req) { 

            if (!this.url.match(/\.json$/)) { 
                req.setRequestHeader("Accept", "application/json");          
            }
        },
        success: function(json) {
            MvcUtil.showSuccessResponse(JSON.stringify(json), link);
        },
        error: function(xhr) {  
            MvcUtil.showErrorResponse(xhr.responseText, link);
        }});
    return false;
});

This function only execute an AJAX call and wait for an HTTP Response passing its content to an handler that will show the output in the view...ok... 该函数仅执行AJAX调用并等待HTTP响应将其内容传递给处理程序,该处理程序将在视图中显示输出...确定...

Before sending the request, the function check if the URL don't end with .json extension 在发送请求之前,该函数检查URL是否不以.json扩展名结尾

If this request don't end with .json extension the following header is added to my HTTP Request: 如果此请求不以.json扩展名结尾,则会将以下标头添加到我的HTTP请求中:

Accept = application/json 接受= application / json

From what I know the Accept Header say which specific mediatype is considerable acceptable for the HTTP Response, in this case say that the only acceptable media type is a JavaScript object having JSON format, ok... 据我所知,Accept Header说HTTP响应可以接受哪种特定的媒体类型,在这种情况下,可以说唯一可接受的媒体类型是具有JSON格式的JavaScript对象,好吧...

This Request is handled from the following method of my controller class that return a valorized object that will be converted in JSON forma using Jaxb2RootElementHttpMessageConverter 此请求是通过我的控制器类的以下方法处理的,该方法返回一个将使用Jaxb2RootElementHttpMessageConverter在JSON格式中转换的有效对象。

@RequestMapping(value="/json", method=RequestMethod.GET)
public ResponseEntity<JavaBean> writeJson() {

    // Oggetto che rappresenta gli HTTP Header dell'HTTP Response
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.TEXT_PLAIN);
    return new ResponseEntity<JavaBean>(new JavaBean("bar", "apple"), headers , HttpStatus.OK);

// return new JavaBean("bar", "apple"); //返回新的JavaBean(“ bar”,“ apple”); } }

Now, my question is about the differences from the two links. 现在,我的问题是关于两个链接的区别。

The first one don't end with .json extension, so the Accept header is added and it is setted on application/json sayng that the browsers expects to receive a JSON object 第一个不以.json扩展名结尾 ,因此添加了Accept标头,并将其设置在application / jsonsayng上,以使浏览器期望接收JSON对象

The second one end with .json extension, so the Jquery method don't set the Accept Header 第二个以.json扩展名结尾 ,因此Jquery方法没有设置Accept Header

But, this thing what mean? 但是,这是什么意思? that when an URL end with .json the Accept header is automatically setted? URL以.json结尾时,是否会自动设置Accept标头? Or more generally, when I have an URL that end with some kind extension (for example like .xml) the relative Accept header is automatically setted? 或更笼统地说,当我有一个以某种扩展名(例如.xml)结尾的URL时,会自动设置相对的Accept标头?

Or simply in this second case, don't set the Accept Header mean don't handle the media type that I can recive in the body of the HTTP Response? 还是仅在第二种情况下,不要设置“接受标头”就意味着不处理我可以在HTTP响应正文中获取的媒体类型?

Ok, your english is not-so-hot, so let me try to help you the best I can. 好的,您的英语不太好,所以让我尽力帮助您。

In my understanding, which may very well be wrong, is that you want to know if the browser will set the Accept: header to be json when the URL ends in json? 以我的理解,这很可能是错误的,您是否想知道当URL以json结尾时浏览器是否将Accept:标头设置为json? I do not believe this is the case. 我认为情况并非如此。 I may be greatly mistaken on this, but you can test this by using something like Firebug or Chrome's Developer Tools, or if you like IE get Fiddler, and see exactly what headers get sent from the browser. 我可能会对此大错特错,但是您可以使用Firebug或Chrome的Developer Tools等工具进行测试,或者如果您喜欢IE获取Fiddler,则可以查看从浏览器发送的标头的确切信息。

Now, if you are asking if Spring will magically put the headers there, then again I think the answer is "no". 现在,如果您要问Spring是否会神奇地将标题放置在此处,那么我再次认为答案是“否”。 The HTTP headers on the request come from the browser, and although you could put in a Servlet Filter or something to set the request filters, I think this would be dangerous to assume all browsers handle these request headers the same way. 请求上的HTTP标头来自浏览器,尽管您可以放入Servlet过滤器或用于设置请求过滤器的东西,但我认为如果假设所有浏览器都以相同的方式处理这些请求标头,那将很危险。

No, if the question is "how are my requests all getting to my Controller's writeJson() method?", then the answer has nothing to do with the "Accept" header at all. 不,如果问题是“我的请求如何全部到达Controller的writeJson()方法?”,那么答案根本与“ Accept”标头无关。 Your method is matching on any URI pattern that ends in /json, and in both cases your URL ends in /json. 您的方法与以/ json结尾的任何URI模式匹配,在两种情况下,您的URL以/ json结尾。 If you want to filter on things that have an "Accept" header of JSON, then I think you want to do something like this: 如果您想过滤具有JSON的“ Accept”标头的内容,那么我认为您想执行以下操作:

@RequestMapping(value="/someUriPattern", headers = {"Accept=application/json"})

Please understand I typed the above from memory, so you may need to tweak it a tad. 请理解,我是从内存中键入以上内容的,因此您可能需要稍作调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM