上次写到我们生成xml,我们想到xml要是可以生成接口不就是可以实现了吗,那我们试一下! 如图所示,wsimport -s +保存地址 -p 包名 +wsdl文档url 生成了一些java类 这时我们可以直接用MyS
上次写到我们生成xml,我们想到xml要是可以生成接口不就是可以实现了吗,那我们试一下!
如图所示,wsimport -s +保存地址 -p 包名 +wsdl文档url
生成了一些java类
这时我们可以直接用MyServiceImpl生成接口
webClient.java
package test;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.MalformedURLException;
import java.net.URL;
/** * Created by caicai on 2016/12/7. */
public class WebClient {
public static void main(String args[]) {
try {
URL url = new URL("http://localhost:8990/test?wsdl");
QName qName = new QName("http://test/", "MyServiceImplService");
Service service=Service.create(url,qName);
MyService myService=service.getPort(MyService.class);
System.out.println("加法:"+myService.add(4,3));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
webTest.java
package test;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.message.Message;
import test.interceptor.AddUserInterceptor;
import test.interceptor.CheckUserInterceptor;
import javax.xml.ws.Endpoint;
import java.util.List;
/** * Created by caicai on 2016/12/7. */
public class WebTest {
public static void main(String args[]){
String address="http://localhost:8990/test";
EndpointImpl endpoint = (EndpointImpl) Endpoint.publish(address, new MyServiceImpl());
//服务端入日志拦截器
List<Interceptor<? extends Message>> inInterceptors = endpoint.getInInterceptors();
inInterceptors.add(new CheckUserInterceptor());
//服务器出日志拦截器
/* List<Interceptor<? extends Message>> outInterceptors = endpoint.getOutInterceptors(); outInterceptors.add(new LoggingOutInterceptor());*/
}
}
这时我们就完成了对webservice的初步认识,同时实践了一下,自后我们 写一个调用webservice的小项目,天气预报!期待中……….