前言:
而今咱们对“apachecxfwebclient”大致比较关心,朋友们都需要知道一些“apachecxfwebclient”的相关文章。那么小编同时在网上搜集了一些有关“apachecxfwebclient””的相关资讯,希望姐妹们能喜欢,我们快快来了解一下吧!1.首先添加必要的依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId></dependency><dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.6</version></dependency><dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.6</version></dependency>
2. 编写webService服务端代码,首先添加webServiceConfig:
package com.example.redisiondemo.config;import com.example.redisiondemo.ws.DemoService;import com.example.redisiondemo.ws.DemoServiceImpl;import org.apache.cxf.Bus;import org.apache.cxf.bus.spring.SpringBus;import org.apache.cxf.jaxws.EndpointImpl;import org.apache.cxf.transport.servlet.CXFServlet;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.xml.ws.Endpoint;@Configurationpublic class CxfConfig { @Bean public ServletRegistrationBean dispatcherServletTest() { return new ServletRegistrationBean(new CXFServlet(),"/demo/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public DemoService demoService() { return new DemoServiceImpl(); } @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), demoService()); endpoint.publish("/api"); return endpoint; }}
3.编写webService 服务端接口:
package com.example.redisiondemo.ws;import javax.jws.WebService;@WebService(name = "DemoService", // 暴露服务名称 targetNamespace = ";// 命名空间,一般是接口的包名倒序)public interface DemoService { public String sayHello(String user);}
4.编写webService 服务端接口实现:
package com.example.redisiondemo.ws;import javax.jws.WebService;import java.util.Date;@WebService(serviceName = "DemoService", // 与接口中指定的name一致 targetNamespace = ";, // 与接口中的命名空间一致,一般是接口的包名倒 endpointInterface = "com.example.redisiondemo.ws.DemoService"// 接口地址)public class DemoServiceImpl implements DemoService { @Override public String sayHello(String user) { return user + ",现在时间:" + "(" + new Date() + ")"; }}
5.然后启动项目访问webService地址:
然后可以看到已经可以访问到了:
6.然后我们再通过springboot initializr创建一个客户端的服务进行webService接口调用,同样需要引入相关依赖:
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.6</version></dependency><dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.6</version></dependency>
7.创建一个测试类对服务端进行调用:
package com.study.hzp.cloud.utils;import org.apache.cxf.endpoint.Client;import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;public class WebServiceTest { public static void main(String[] args) throws Exception { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient(";); Object[] objects = client.invoke("sayHello","张三"); //输出调用结果 System.out.println(objects[0].getClass()); System.out.println(objects[0].toString()); }}
8.成功调用,打印出结果信息
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #apachecxfwebclient