如何在 Netflix DGS 解析器中获取标头信息

问题描述

我们可以编写如下的查询解析器层

@DgsData(parentType = "Query",field = "answersByQuestionUuid")
    public List<Answer> answersByQuestionUuid(@InputArgument("questionUuid") UUID questionUuid,@InputArgument("enhancedContent") boolean enhancedContent,@InputArgument("templateName") String templateName)  {
        if (enhancedContent) {
            return getStructuredAnswersByQuestionUUID(questionUuid.toString(),templateName);
        }
        return getAnswersByQuestionUUID(questionUuid);
    }

如何在解析器中获取 HTTP 标头。

解决方法

除了 DGS 输入参数之外,您还可以使用 Spring 框架中的 @RequestHeader 注释来接收 HTTP 请求标头值。例如:

    public List<Answer> answersByQuestionUuid(@InputArgument("questionUuid") UUID questionUuid,@RequestHeader("Content-Type") String contentType)  {