【问题标题】:How to get JAX-WS response HTTP status code如何获取 JAX-WS 响应 HTTP 状态码
【发布时间】:2017-03-24 13:12:55
【问题描述】:

调用 JAX-WS 端点时,如何获取 HTTP 响应代码?

在下面的示例代码中,在port.getCustomer(customerID); 调用Web 服务时可能会抛出异常,例如401500

在这种情况下,如何从 HTTP 响应中获取 HTTP 状态码?

@Stateless
public class CustomerWSClient {

    @WebServiceRef(wsdlLocation = "/customer.wsdl")
    private CustomerService service;

    public void getCustomer(Integer customerID) throws Exception {
        Customer port = service.getCustomerPort();
        port.getCustomer(customerID); // how to get HTTP status           
    }

}

【问题讨论】:

  • 您是否尝试过捕获异常?

标签: java jax-ws


【解决方案1】:

完成@Praveen 答案后,您必须将port 转换为原始BindingProvider,然后从上下文中获取值。

不要忘记,如果您的托管 Web 服务客户端发生异常,事务将被标记为回滚。

@Stateless
public class CustomerWSClient {

    @WebServiceRef(wsdlLocation = "/customer.wsdl")
    private CustomerService service;

    public void getCustomer(Integer customerID) throws Exception {
        Customer port = service.getCustomerPort();
        try {
            port.getCustomer(customerID);  
        } catch(Exception e) {
            throw e;
        } finally {
            // Get the HTTP code here!
            int responseCode = (Integer)((BindingProvider) port).getResponseContext().get(MessageContext.HTTP_RESPONSE_CODE);
        }
    }

}

【讨论】:

    【解决方案2】:

    以下帖子与您的问题相似。希望它对你有用

    https://stackoverflow.com/a/35914837/4896191

    【讨论】:

    • 它工作得很好,不知道我在搜索了很多之后没有遇到它。我猜我对 JAX-WS 的了解并没有我想象的那么多。
    猜你喜欢
    • 1970-01-01
    • 2015-05-21
    • 2019-04-06
    • 2021-03-15
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多