How to get JAX-WS response HTTP status code

前端 未结 2 2020
傲寒
傲寒 2021-01-21 22:56

When calling a JAX-WS endpoint, how can I get the HTTP response code?

In the sample code bellow, when calling the web service at port.getCustomer(customerID);

2条回答
  •  臣服心动
    2021-01-21 23:19

    Completing @Praveen answer, you have to turn the port into a raw BindingProvider and then get the values from the context.

    Don't forget that transaction will be marked for rollback if an exception occours in your managed web service client.

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

提交回复
热议问题