首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GraphQL + Springboot + Http报头

GraphQL + Springboot + Http报头
EN

Stack Overflow用户
提问于 2022-01-20 18:07:33
回答 3查看 849关注 0票数 2

我正在使用以下graphql依赖关系:

代码语言:javascript
复制
    "com.graphql-java-kickstart:graphql-spring-boot-starter:12.0.0",
    "com.graphql-java-kickstart:graphql-java-tools:12.0.0", 

以及如何从httpRequestHeaders类中获取DataFetchingEnvironment。我认为DataFetchingEnvironment.getContext()是不可取的,还有其他的选择吗?

当前逻辑:

代码语言:javascript
复制
        GraphQLServletContext servletContext = env.getContext(); //deprecated

        GraphQLContext qlContext = env.getGraphQlContext(); // No httpRequest

        var httpRequest = servletContext.getHttpServletRequest();
EN

回答 3

Stack Overflow用户

发布于 2022-05-05 13:35:46

你可以:

代码语言:javascript
复制
env.getGraphQlContext().get(HttpServletRequest.class).getHeader("header")
票数 0
EN

Stack Overflow用户

发布于 2022-08-18 14:12:09

从12.0.0开始,不支持environment.getGraphQLContext()。该项目的github (https://github.com/graphql-java-kickstart/graphql-spring-boot/issues/808)中引用了这种缺乏支持的情况。

从13.0.0版本开始,您可以通过以下方式访问HttpServletRequest

代码语言:javascript
复制
HttpServletRequest httpServletRequest = environment.getGraphQlContext().get(HttpServletRequest.class);

如果您需要使用12.0.0版本,我的建议是使用OncePerRequestFilter并将所需的头信息存储在ThreadLocal值中。只需记住在请求被处理后清除线程本地值。只有在关闭11.0.0中引入的GraphQL库在不同线程中处理请求筛选器和graphql解析器时,“异步模式”才能工作。

票数 0
EN

Stack Overflow用户

发布于 2022-09-20 10:16:44

升级到13.0.0 (或14.0.0)并调用

env.getGraphQlContext().get(HttpServletRequest.class);HttpServletRequest httpServletRequest =

这将给你和HttpServletRequest,从那里你可以打电话

代码语言:javascript
复制
private static Map<String, String> getHeadersFromRequest(HttpServletRequest httpServletRequest) {
        var mapOfHeaders = new HashMap<String, String>();

        Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String headerName = headerNames.nextElement();
                mapOfHeaders.put(headerName, httpServletRequest.getHeader(headerName));
            }
        }
        return mapOfHeaders;
    }

若要返回标题列表映射,请执行以下操作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70791293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档