调用JAX-WS端点时,如何获取HTTP响应代码?
在下面的示例代码中,在调用Web服务时port.getCustomer(customerID);可能会引发Exception,例如401或500。
port.getCustomer(customerID);
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答案后,您必须将变成port原始BindingProvider值,然后从上下文中获取值。
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); } } }