小编典典

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

java

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

在下面的示例代码中,在调用Web服务时port.getCustomer(customerID);可能会引发Exception,例如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           
    }

}

阅读 344

收藏
2020-11-26

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

}
2020-11-26