关键点:
1:类上要注解@WebService
2:方法上注解@WebMethod,方法参数:@WebParam来接
3:发布配置:
@Bean
public ServletRegistrationBean dispatcherServlet() {
ServletRegistrationBean sbean = new ServletRegistrationBean(new CXFServlet(), "/demo/WebServices/*");
return sbean;
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public DemoService getService() {
return new DemoService();
}
@Bean
public Endpoint endpoint() {
EndpointImpl point = new EndpointImpl(springBus(), getService());
point.publish("demoWebService");
return point;
}
4:启动项目,生成wsdl: http://127.0.0.1:8088/demo/WebServices/demoWebService?wsdl
5:调用:
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://127.0.0.1:8088/demo/WebServices/demoWebService?wsdl");
Object[] objects = new Object[0];
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("test001", "01");
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("process", jsonObject.toString(), "123");
System.out.println("objects[0]:" + objects[0]);
} catch (Exception e) {
e.printStackTrace();
}
6:原理
webservice是一种通讯技术,wsdl 是一个xml文件,描述接口信息,使用soap来提供服务。