今天看到一个项目要和工厂的ERP进行对接,用到了webservice。虽然使用用springboot较为方便,还是了解一下:
网上的解释很多,其实就是跨语言和操作系统的的远程调用技术。比如亚马逊,可以将自己的服务以webservice的服务形式暴露出来,我们就可以通过web调用这些,无论我们使用的语言是java还是c,这也是SOA应用一种表现形式。
WSDL(Web Services Description Language)将无论用何种语言书写的web service描述出来,比如其参数或返回值。WSDL是服务端和客户端都能解读的标准格式。客户端通过URL地址访问到WSDL文件,在调用服务端之前先访问WSDL文件。 读取到WSDL后通过客户端的API类可以生成代理类,调用这些代理类就可以访问webservice服务。代理类将客户端的方法变为soap(Simple Object Access Protocol,可以理解为http+xml)格式通过http发送,同时接受soap格式的返回值并解析。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.4</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.1.11</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.11.0</version> </dependency> </dependencies> 复制代码
public class TestBean { private String name; private String id; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } } 复制代码
@WebService(name = "testService",targetNamespace = "http://service.webservicedemo.sxt.com") public interface Wbservice { @WebMethod @WebResult() public String helloService(@WebParam(name = "name") String name); @WebMethod public ArrayList<TestBean> getAllBean() } 复制代码
@WebService(name = "testService", targetNamespace = "http://service.webservicedemo.sxt.com", endpointInterface="com.sxt.webservicedemo.Service.Wbservice") @Component public class WebserviceImpl implements Wbservice { @Override public String helloService(String name) { return "hello"+name; } @Override public ArrayList<TestBean> getAllBean() { ArrayList<TestBean> list = new ArrayList<>(); TestBean bean1 = new TestBean("zhangsan", "1"); TestBean bean2 = new TestBean("lisi", "2"); list.add(bean1); list.add(bean2); return list; } } 复制代码
@Configuration public class Webserviceconfig { @Bean public ServletRegistrationBean dispatcherServlet(){ return new ServletRegistrationBean(new CXFServlet(),"/service/*");//发布服务名称 } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public Wbservice beanService() { return new WebserviceImpl(); } @Bean public Endpoint endpoint() { EndpointImpl endpoint=new EndpointImpl(springBus(), beanService());//绑定要发布的服务 endpoint.publish("/test"); //显示要发布的名称 return (Endpoint) endpoint; } 复制代码
public static void main(String[] args) throws Exception { JaxWsDynamicClientFactory dcflient=JaxWsDynamicClientFactory.newInstance(); Client client=dcflient.createClient("http://localhost:8080/service/test?wsdl"); Object[] objects=client.invoke("getBean","411001"); System.out.println("*******"+objects[0].toString()); } 复制代码