问题描述
调用JAX-WS端点时,如何获取HTTP响应代码?
When calling a JAX-WS endpoint, how can I get the HTTP response code?
在下面的示例代码中,当在port.getCustomer(customerID);
处调用Web服务时,可能会引发异常,例如401
或500
.
In the sample code bellow, when calling the web service at port.getCustomer(customerID);
an Exception may be thrown, such as 401
or 500
.
在这种情况下,如何从HTTP响应中获取HTTP状态代码?
In such cases, how can I get the HTTP status code from the HTTP response?
@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
,然后从上下文中获取值.
Completing @Praveen answer, you have to turn the port
into a raw BindingProvider
and then get the values from the context.
如果托管的Web服务客户端中发生异常,请不要忘记将事务标记为回滚.
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);
}
}
}
这篇关于如何获取JAX-WS响应HTTP状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!