带有equals的Spring @RequestMapping标头 [英] Spring @RequestMapping header with equals

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

问题描述

我想用Spring @RequestMapping和header属性来检测值 application / json; version = 1的 Accept 头。* 。该计划将为版本2映射另一个方法,该版本的值将为 application / json; version = 2。*

春天似乎忽略了版本值。我猜它是把等号看作是另一个标题属性。



有没有办法解决这个问题?



附注:


  1. 我无法更新Spring版本以支持消耗属性

  2. 我无法更改请求标头的格式

  3. ol>

    解决方案

    似乎spring requestmapping会忽略媒体类型参数。
    您可以通过手动将请求路由到您的首选终端来解决此问题。

      @RequestMapping(value =/ ),header =Accept = application / json)
    @ResponseBody
    字符串请求(@RequestHeader HttpHeaders标题){
    for(MediaType mediaType:headers.getAccept()){$ b $如果(mediaType.isCompatibleWith(MediaType.APPLICATION_JSON)){
    if(mediaType.getParameter(version)。startsWith(1。)){
    return v1();
    } else if(mediaType.getParameter(version)。startsWith(2。)){
    return v2();
    }
    }
    }
    返回错误;
    }


    I want to use Springs @RequestMapping with the header attribute to detect an Accept header with the value application/json;version=1.*. The plan is to have another method mapped similarly for version 2 which will have the value application/json;version=2.*.

    Spring seems to be ignoring the version value. I'm guessing it's treating the equals sign as another header attribute.

    Is there a way around this?

    Side notes:

    1. I can't update the Spring version to support the consumes attribute
    2. I can't change the format the request header will come in

    解决方案

    It seems that the spring requestmapping ignores media type parameters. You can work around this by manually routing the request to your preferred endpoint.

    @RequestMapping(value = "/", headers = "Accept=application/json")
    @ResponseBody
    String request(@RequestHeader HttpHeaders headers){
        for(MediaType mediaType : headers.getAccept()){
            if(mediaType.isCompatibleWith(MediaType.APPLICATION_JSON)){
                if(mediaType.getParameter("version").startsWith("1.")){
                    return v1();
                }else if(mediaType.getParameter("version").startsWith("2.")){
                    return v2();
                }
            }
        }
        return "error";
    }
    

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

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