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

时间:2017-03-24 13:12:56

标签: java jax-ws

调用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           
    }

}

2 个答案:

答案 0 :(得分:2)

完成@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);
        }
    }

}

答案 1 :(得分:0)

以下帖子与您的问题类似。希望它应该适合你

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