如何在Spring中解析HTTP的Accept头?

huangapple go评论102阅读模式
英文:

How parse an http accept header in spring?

问题

我想在Spring中解析HTTP接受头,以确定是否可以返回JSON。我正在尝试使用以下代码。

  1. class MediaTypeUtil {
  2. private final static Logger logger = LoggerFactory.getLogger(MediaTypeUtil.class);
  3. static boolean acceptsJson(HttpServletRequest request) {
  4. try {
  5. String accept = request.getHeader("Accept");
  6. MediaType requestType = MediaType.valueOf(accept);
  7. return MediaType.APPLICATION_JSON.isCompatibleWith(requestType);
  8. } catch (InvalidMediaTypeException e) {
  9. logger.debug("MediaType parsing error", e);
  10. return false;
  11. }
  12. }
  13. }

当请求的接受头值为application/json、application/javascript、text/javascript、text/json时,我遇到了异常:

  1. Caused by: org.springframework.util.InvalidMimeTypeException: Invalid mime type "application/json, application/javascript, text/javascript, text/json": Invalid token character ',' in token "json, application/javascript, text/javascript, text/json"
  2. at org.springframework.util.MimeTypeUtils.parseMimeTypeInternal(MimeTypeUtils.java:262)

此代码是从Servlet过滤器中使用的,因此我不能依赖于SpringMVC注释。

Spring是否有一种方法来解析接受头,并确定它是否与特定媒体类型兼容?

英文:

I want to parse an HTTP accept header in Spring to determine if I can send back JSON. I am trying with the following code.

  1. class MediaTypeUtil {
  2. private final static Logger logger = LoggerFactory.getLogger(MediaTypeUtil.class);
  3. static boolean acceptsJson(HttpServletRequest request) {
  4. try {
  5. String accept = request.getHeader("Accept");
  6. MediaType requestType = MediaType.valueOf(accept);
  7. return MediaType.APPLICATION_JSON.isCompatibleWith(requestType);
  8. } catch (InvalidMediaTypeException e) {
  9. logger.debug("MediaType parsing error",e);
  10. return false;
  11. }
  12. }
  13. }

When a request arrives with accept header value of application/json, application/javascript, text/javascript, text/json I end up with an exception

  1. Caused by: org.springframework.util.InvalidMimeTypeException: Invalid mime type "application/json, application/javascript, text/javascript, text/json": Invalid token character ',' in token "json, application/javascript, text/javascript, text/json"
  2. at org.springframework.util.MimeTypeUtils.parseMimeTypeInternal(MimeTypeUtils.java:262)

This code is being used from a Servlet Filter so I can't rely on SpringMVC annotations.

Does Spring has a method for parsing accept header and determining if it is compatible with a specific media type?

答案1

得分: 5

春天本身就像您一样在内部执行此操作(即从请求中获取ACCEPT标头),但他们将其提供给此调用:

  1. MediaType.parseMediaTypes(get(ACCEPT));

这将返回一个您需要处理的 List<MediaType>

英文:

Spring itself does it exactly like you internally (i.e. getting the ACCEPT header from the request), but they feed it to this call:

  1. MediaType.parseMediaTypes(get(ACCEPT));

Which will return you a List&lt;MediaType&gt; that you need to work with.

huangapple
  • 本文由 发表于 2020年3月16日 01:45:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/60695858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定