springboot整合webservice_Springboot整合webService居然可以这么简单

本文详细介绍了如何使用Springboot整合Webservice,从添加依赖、配置webService服务端、编写接口及其实现,到启动项目并成功调用服务端接口,展示了整个过程的简洁与高效。
摘要由CSDN通过智能技术生成

1.首先添加必要的依赖:

org.springframework.boot    spring-boot-starter-web-servicesorg.apache.cxf    cxf-rt-frontend-jaxws    3.1.6org.apache.cxf    cxf-rt-transports-http    3.1.6
75659b13fcbf7bba8ab61c5a53d08c8d.png

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 = "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

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

1131fc0e293995db55958273067fe8f6.png

6.然后我们再通过springboot initializr创建一个客户端的服务进行webService接口调用,同样需要引入相关依赖:

org.apache.cxf    cxf-rt-frontend-jaxws    3.1.6org.apache.cxf    cxf-rt-transports-http    3.1.6

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.成功调用,打印出结果信息

cdf2f3a0f01feba5a3c8fedb28f495b6.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值