在SpringBoot Web服务端点中,如何访问HTTP标头?

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

In SpringBoot web service endpoint, how do I access http headers?

问题

我有一个用 @Endpoint 注解的类和一个处理方法

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getWeatherRequest")
@SoapAction("http://foo/domain/getWeatherRequest")
@ResponsePayload
public GetWeatherResponse getWeatherRequest(@RequestPayload GetWeatherRequest request) {
    // 我想在这里获取 HTTP 头部(不是 SOAP 头部)
}
英文:

I have a class annotated with @Endpoint and a handler method

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getWeatherRequest")
@SoapAction("http://foo/domain/getWeatherRequest")
@ResponsePayload
public GetWeatherResponse getWeatherRequest(@RequestPayload GetWeatherRequest request) {
    // I want to get HTTP header (Not SOAP Header) here
}

答案1

得分: 1

@RequestHeader 注解用法示例:

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getWeatherRequest")
@SoapAction("http://foo/domain/getWeatherRequest")
@ResponsePayload
public GetWeatherResponse getWeatherRequest(@RequestPayload GetWeatherRequest request,
                                            @RequestHeader("header-name") String header) {

}
英文:

Try using the @RequestHeader annotation

@SoapAction("http://foo/domain/getWeatherRequest")
@ResponsePayload
public GetWeatherResponse getWeatherRequest(@RequestPayload GetWeatherRequest request, 
@RequestHeader("header-name") String header) {
   
}

</details>



# 答案2
**得分**: 0

你可以在方法中使用`RequestHeader`注解来访问HTTP头部,你可以使用`required`属性来指定头部是必填还是可选的。

```java
@RequestHeader(value = "ConfigId", required = true) String configId
英文:

You can use RequestHeader annotation in your method to access http headers, you can specify whether header is mandatory or optional using required attribute

@RequestHeader(value = &quot;ConfigId&quot;, required = true) String configId

huangapple
  • 本文由 发表于 2020年10月1日 09:52:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64148039.html
匿名

发表评论

匿名网友

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

确定