Springboot WebService例子

我是在原有公司项目上测试
上网搜索等做了测试了一个webService demo
和很多人的一样
但是我看很多人的都运行不起来
记录一下 以后也好复习
刚毕业半年的小白

在和controller同层中建立package WebService
在里面建立webService接口

webservice接口:

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;


/**
 * WebService接口
 * @author 
 * @date 2019/12/02
 */
@WebService(name = "TestService" ,  //暴露服务名称
        targetNamespace = "http://www.WebService.menopause.kedauis.com") //命名空间,一般为包名倒叙
public interface TestService {
    @WebMethod
    @WebResult(name = "String",targetNamespace = "")
    public String helloWorld(@WebParam(name = "HelloName") String name);
}

WebService 包中建立webService实现类


import org.springframework.stereotype.Component;
import javax.jws.WebService;


/**
 * 接口实现
 * @author 
 *
 */
@WebService(name = "TestService" ,  //暴露服务名称
        targetNamespace = "http://www.WebService.menopause.kedauis.com" , //命名空间,一般为包名倒叙
        endpointInterface = "com.kedauis.menopause.WebService.TestService")
@Component
public class TestServiceImpl implements TestService {
    @Override
    public String helloWorld(String name) {
        return "Hello World!!! --->"+name;
    }
}

在同级别中建立config包
包内建立webConfig

刚开始我上网找到这段代码
但是启动springboot的时候会报错
bus注入失败 找不到 欢迎大佬告诉我为什么

 @Autowired
    private Bus bus;

    @Autowired
    TestService service;

    /*jax-ws*/
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, service);
        endpoint.publish("/api");
        return endpoint;
    }

所以我就在网上找到了另一种方法

import com.kedauis.menopause.WebService.TestService;
import com.kedauis.menopause.WebService.TestServiceImpl;
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.Configuration;
import org.springframework.context.annotation.Bean;

import javax.xml.ws.Endpoint;


@Configuration
public class WebConfig {
   
    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(),"/demo/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public TestService demoService() {
        return new TestServiceImpl() {
        };
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
        //http://localhost:8888/demo/TestService?wsdl
        endpoint.publish("/TestService");
        return endpoint;
    }
}

启动Application
访问网址 http://localhost:8888/demo/TestService?wsdl
访问成功

此处webservice服务器端建立完成!

客户端部分:

仅用于测试与练习
可在同层建立Client文件夹
加入测试代码 (可用@Test) 本人直接main方法 测试:

import com.kedauis.menopause.WebService.TestService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;


public class CxfClient {
    public static void main(String[] args) {
        test1();
    }

    /**
     * 方式1.代理类工厂的方式,需要拿到对方的接口
     */
    public static void test1() {
        try {
            // 接口地址
            String address = "http://localhost:8888/demo/TestService?wsdl";
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(TestService.class);
            // 创建一个代理接口实现
            TestService cs = (TestService) jaxWsProxyFactoryBean.create();
            // 数据准备
            String HelloName = "Leftso";
            // 调用代理接口的方法调用并返回结果
            String result = cs.helloWorld(HelloName);
            System.out.println("返回结果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void test2(Integer a) {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8888/demo/TestService?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,
        // PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("helloWorld", "Leftso");
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值