代码之家  ›  专栏  ›  技术社区  ›  Juha Syrjälä

如何访问Spring ws端点中的HTTP头?

  •  7
  • Juha Syrjälä  · 技术社区  · 14 年前

    我的代码如下:

    public class MyEndpoint extends AbstractMarshallingPayloadEndpoint {
      protected Object invokeInternal(Object arg) throws Exception {
          MyReq request = (MyReq) arg;
          // need to access some HTTP headers here
          return createMyResp();
      }
    }
    

    invokeInternal() 仅获取未编组的JAXB对象作为参数。如何访问内部请求附带的HTTP头 ?

    ThreadLocal 然后在内部访问的变量 调用内部() ,但是有没有更好的、更像春天的方法来做这个呢?

    3 回复  |  直到 14 年前
        1
  •  16
  •   M. Deinum    6 年前

    您可以添加这些方法。这个 TransportContextHolder 将在线程局部变量中保存一些与传输(本例中为HTTP)相关的数据。您可以访问 HttpServletRequest TransportContext .

    protected HttpServletRequest getHttpServletRequest() {
        TransportContext ctx = TransportContextHolder.getTransportContext();
        return ( null != ctx ) ? ((HttpServletConnection ) ctx.getConnection()).getHttpServletRequest() : null;
    }
    
    protected String getHttpHeaderValue( final String headerName ) {
        HttpServletRequest httpServletRequest = getHttpServletRequest();
        return ( null != httpServletRequest ) ? httpServletRequest.getHeader( headerName ) : null;
    }
    
        2
  •  1
  •   Vladimir    6 年前

    您可以通过注入 HttpServletRequest请求 .

    例如,你需要 自动化 头(使用基本身份验证)。

    SOAP请求:

    POST http://localhost:8025/ws HTTP/1.1
    Accept-Encoding: gzip,deflate
    Content-Type: text/xml;charset=UTF-8
    SOAPAction: ""
    Authorization: Basic YWRtaW46YWRtaW4=
    Content-Length: 287
    Host: localhost:8025
    Connection: Keep-Alive
    User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
    
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tok="http://abcdef.com/integration/adapter/services/Token">
       <soapenv:Header/>
       <soapenv:Body>
          <tok:GetTokenRequest>
          </tok:GetTokenRequest>
       </soapenv:Body>
    </soapenv:Envelope>
    

    java类

    @Endpoint
    @Slf4j
    public class TokenEndpoint {
    
        public static final String NAMESPACE_URI = "http://abcdef.com/integration/adapter/services/Token";
        private static final String AUTH_HEADER = "Authorization";
    
        private final HttpServletRequest servletRequest;
        private final TokenService tokenService;
    
        public TokenEndpoint(HttpServletRequest servletRequest, TokenService tokenService) {
            this.servletRequest = servletRequest;
            this.tokenService = tokenService;
        }
    
        @PayloadRoot(namespace = NAMESPACE_URI, localPart = "GetTokenRequest")
        @ResponsePayload
        public GetTokenResponse getToken(@RequestPayload GetTokenRequest request) {
            String auth = servletRequest.getHeader(AUTH_HEADER);
            log.debug("Authorization header is {}", auth);
            return tokenService.getToken(request);
        }
    }
    
        3
  •  0
  •   Community CDub    7 年前

    我也有同样的问题(看这个 other question ). 我需要在WS中添加一个内容类型头。我走上了Servlet过滤器的道路。大多数情况下,您不需要更改web服务中的HTTP头。但是。。。有时理论和实践之间有分歧。