内容简介:今天看到一个项目要和工厂的ERP进行对接,用到了webservice。虽然使用用springboot较为方便,还是了解一下:网上的解释很多,其实就是跨语言和操作系统的的远程调用技术。比如亚马逊,可以将自己的服务以webservice的服务形式暴露出来,我们就可以通过web调用这些,无论我们使用的语言是java还是c,这也是SOA应用一种表现形式。WSDL(Web Services Description Language)将无论用何种语言书写的web service描述出来,比如其参数或返回值。WSDL是服
今天看到一个项目要和工厂的ERP进行对接,用到了webservice。虽然使用用springboot较为方便,还是了解一下:
webservice是什么
网上的解释很多,其实就是跨语言和操作系统的的远程调用技术。比如亚马逊,可以将自己的服务以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格式的返回值并解析。
webservice使用
-
创建工程
-
添加依赖
<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>
复制代码
-
测试对象TestBean
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());
}
复制代码
以上所述就是小编给大家介绍的《SpringBoot+webservice》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Learn Python the Hard Way
Zed A. Shaw / Addison-Wesley Professional / 2013-10-11 / USD 39.99
Master Python and become a programmer-even if you never thought you could! This breakthrough book and CD can help practically anyone get started in programming. It's called "The Hard Way," but it's re......一起来看看 《Learn Python the Hard Way》 这本书的介绍吧!