Java 如何获取JAX-WS响应HTTP状态代码
调用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
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的了解没有我想象的那么多。