我想在我的客户端应用程序上从WSDL调用SOAP服务。我使用wsimport
命令从WSDL生成所需的类。
wsimport -keep -verbose <wsdladdress>
然后我创建了一个基类,它将成为wsdl(端点,授权等)的映射器
BaseService.java
public abstract class BaseService {
private String wsUsername = "";
private String wsPassword = "";
private static final Logger logger = Logger.getLogger(BaseService.class);
protected Emoneytopup emoneyTopup = new Emoneytopup();
protected TopupWebService topupWebService;
public static String END_POINT_TARGET_TOPUP = "";
public BaseService(){
try{
wsUsername = PropertiesHandler.getPromoEngineProperties().getProperty("topupUsername");
wsPassword = PropertiesHandler.getPromoEngineProperties().getProperty("topupPassword");
END_POINT_TARGET_TOPUP = PropertiesHandler.getPromoEngineProperties().getProperty("topupEndPoint");
String wsAuth = (wsUsername.length()>0&&wsPassword.length()>0) ? this.wsUsername + ":" + this.wsPassword : "";
// Authentication header
Map headers = new HashMap<>();
//headers.put("Authorization", Collections.singletonList("Basic " + new BASE64Encoder().encode(auth.getBytes())));
headers.put("Authorization", Collections.singletonList("Basic " + wsAuth.getBytes()));
// TopUp Web Service
this.topupWebService = this.emoneyTopup.getEmoneytopupPort();
((BindingProvider)this.topupWebService).getRequestContext().put("javax.xml.ws.service.endpoint.address", END_POINT_TARGET_TOPUP);
((BindingProvider)this.topupWebService).getRequestContext().put("javax.xml.ws.http.request.headers", headers);
} catch(IOException ie){
logger.error("Cannot get variables from properties file");
}
}
protected void printInMessage(Class c, Object o) {
try {
Marshaller marshaller = JAXBContext.newInstance(new Class[] { c }).createMarshaller();
StringWriter stringWriter = new StringWriter();
marshaller.marshal(o, stringWriter);
String trxContent = stringWriter.toString();
logger.debug(trxContent);
//this.loggingService.saveMsgIn("", "WS", trxContent);
} catch (Exception e) {
logger.error(e, e);
}
}
protected void printInMessage(Object o) {
printInMessage(o.getClass(), o);
}
protected void printOutMessage(Class c, Object o) {
try {
Marshaller marshaller = JAXBContext.newInstance(new Class[] { c }).createMarshaller();
StringWriter stringWriter = new StringWriter();
marshaller.marshal(o, stringWriter);
String trxContent = stringWriter.toString();
logger.debug(trxContent);
//this.loggingService.saveMsgOut("", "WS", trxContent);
} catch (Exception e) {
this.logger.error(e, e);
}
}
protected void printOutMessage(Object o) {
printOutMessage(o.getClass(), o);
}
protected <T> T getValue(JAXBElement<T> jaxbe) {
if(jaxbe != null)
return jaxbe.getValue();
return null;
}
}
然后,我创建了另一个类ESCreditRunner.java
,它扩展了上面的映射器类。
ESCreditRunner.java
public class ESCreditRunner extends BaseService{
private File statusFile;
private Properties status;
private static final Logger logger = Logger.getLogger(ESCreditRunner.class);
public ESCreditRunner(File stFile, Properties st){
this.statusFile = stFile;
this.status = st;
}
public ESCreditRunner(){}
public void testCall(){
System.out.println("Attempting to run testCall()..");
TopupInquiryRequest request = new TopupInquiryRequest();
this.topupWebService.inquiry(request);
}
}
这是调用ESCreditRunner类
EngineRunner.java
public class EngineRunner {
private static final Logger logger = Logger.getLogger(EngineRunner.class);
private static File queriesFile;
private static Properties queries;
private static File statusFile;
private static Properties status;
private static ESClient esClient = new ESTransportClient();
public static void main(String[] args){
logger.info("Promo Engine started by Scheduler..");
ESCreditRunner cRunner = new ESCreditRunner();
cRunner.testCall();
}
}
当我运行主类时,在服务器应用程序上使用调试模式(如上所示在TopUpWebService上),请求没有到达服务器。我也得到以下错误:
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Server error: unexpected-error
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:156)
at com.sun.proxy.$Proxy36.inquiry(Unknown Source)
at com.ptdam.promo.services.ESCreditRunner.testCall(ESCreditRunner.java:64)
at com.ptdam.promo.runners.EngineRunner.main(EngineRunner.java:44)
Caused by: org.apache.cxf.binding.soap.SoapFault: Server error: unexpected-error
at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.unmarshalFault(Soap11FaultInInterceptor.java:75)
at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.handleMessage(Soap11FaultInInterceptor.java:46)
at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.handleMessage(Soap11FaultInInterceptor.java:35)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
at org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:113)
at org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:69)
at org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:34)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:798)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1667)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1520)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1428)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:658)
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:532)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:464)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:367)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:320)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:89)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)
... 3 more
是什么导致了这个错误,我怎么能弄明白,以及解决方案是什么? 任何帮助将不胜感激:)