Java 如何获取JAX-WS响应HTTP状态代码

Java 如何获取JAX-WS响应HTTP状态代码,java,jax-ws,Java,Jax Ws,调用JAX-WS端点时,如何获取HTTP响应代码 在下面的示例代码中,当在port.getCustomer(customerID)调用web服务时可能会引发异常,例如401或500 在这种情况下,如何从HTTP响应中获取HTTP状态代码 @Stateless public class CustomerWSClient { @WebServiceRef(wsdlLocation = "/customer.wsdl") private CustomerService service

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

在下面的示例代码中,当在
port.getCustomer(customerID)调用web服务时可能会引发异常,例如
401
500

在这种情况下,如何从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           
    }

}

下面的帖子与你的问题类似。希望它对你有用


完成@Praveen answer后,必须将
端口
转换为原始
绑定提供程序
,然后从上下文中获取值

不要忘记,如果托管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);
        }
    }

}

你试过捕获异常吗?它工作得很好,不确定我在搜索了很多次之后怎么没有遇到它。我想我对JAX-WS的了解没有我想象的那么多。