快捷搜索:  汽车  科技

spring boot发布webservice接口:Springboot整合webService居然可以这么简单

spring boot发布webservice接口:Springboot整合webService居然可以这么简单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("http://localhost:8080/demo/api?w

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>

spring boot发布webservice接口:Springboot整合webService居然可以这么简单(1)

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; @Configuration public 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 = "http://ws.redisiondemo.example.com"// 命名空间 一般是接口的包名倒序 ) 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 = "http://ws.redisiondemo.example.com" // 与接口中的命名空间一致 一般是接口的包名倒 endpointInterface = "com.example.redisiondemo.ws.DemoService"// 接口地址 ) public class DemoServiceImpl implements DemoService { @Override public String sayHello(String user) { return user ",现在时间:" "(" new Date() ")"; } }

5.然后启动项目访问webService地址:

http://localhost:8080/demo/api?wsdl

然后可以看到已经可以访问到了:

spring boot发布webservice接口:Springboot整合webService居然可以这么简单(2)

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("http://localhost:8080/demo/api?wsdl"); Object[] objects = client.invoke("sayHello" "张三"); //输出调用结果 System.out.println(objects[0].getClass()); System.out.println(objects[0].toString()); } }

8.成功调用,打印出结果信息

spring boot发布webservice接口:Springboot整合webService居然可以这么简单(3)

猜您喜欢: