gpt4 book ai didi

java - servlet 支持两种输出格式的标准做法

转载 作者:行者123 更新时间:2023-12-04 04:55:32 25 4
gpt4 key购买 nike

直到现在;我们的 servlet 仅用于支持 application/json作为输出格式。事实上;我们刚刚将输出返回为 application/json ;不考虑用户的要求:(

前进;我们将支持多种输出格式:json/xml 等。
我想知道;实现这一目标的行业惯例是什么?我们只是阅读getContentType()来自 HttpServletRequest并返回适当的数据格式?

最佳答案

getContentType()返回 Content-Type请求的 header 值。这指定了 HTTP 请求正文的内容。不是它期望作为响应的内容类型。

HTTP 请求可以具有 Accept 标题集

to specify certain media types which are acceptable for the response. Accept headers can be used to indicate that the request is specifically limited to a small set of desired types, as in the case of a request for an in-line image.



用作
Accept: text/plain 
// or application/xml or a comma separated list for multiple types (which you would have to parse)

来自 HttpServletRequest ,您可以将其检索为
String responseContentType = request.getHeader("Accept");

Javadoc for HttpServletRequest#getHeader(String)here .

显然,如果您的客户端是浏览器,则更改标题有点困难。

关于java - servlet 支持两种输出格式的标准做法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16801786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com