首页 > Web开发 > 详细

01_websevice接口示例

时间:2021-07-19 00:26:04      阅读:26      评论:0      收藏:0      [点我收藏+]
webservice接口的服务端和客户端调用示例
 
服务端
1.pom文件坐标
<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.CxfConfig.java配置文件
package com.demo.producer.config;

import com.demo.producer.service.CodebtIndexService;
import com.demo.producer.service.CodebtIndexServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.service.model.EndpointInfo;
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 dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(),"/services/*");
        //application.yml
    }

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

    @Bean
    public CodebtIndexService codebtIndexService() {
        return new CodebtIndexServiceImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointInfo endpointInfo = new EndpointInfo();
        Endpoint endpoint = new EndpointImpl(springBus(),codebtIndexService());
        endpoint.publish("/CodebtIndex");
        return endpoint;
    }

}

 

3.CodebtIndexService.java 接口

package com.demo.producer.service;

import javax.jws.WebService;

@WebService(name = "CodebtIndexService", // 暴露服务名称
        targetNamespace = "http://service.cib.com"// 命名空间,一般是接口的包名倒序
)
public interface CodebtIndexService {

    public String codebtIndex(String carId);

}

 

4.CodebtIndexServiceImpl.java 接口实现类

package com.demo.producer.service;

import javax.jws.WebService;

@WebService(serviceName = "CodebtIndexService", // 与接口中指定的name一致
        targetNamespace = "http://service.cib.com", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.demo.producer.service.CodebtIndexService"// 接口地址
)
public class CodebtIndexServiceImpl implements CodebtIndexService {

    @Override
    public String codebtIndex(String carId) {
        return carId + ":共债指数:50" ;
    }

}

 

客户端
1.pom文件

<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.调用

public static void main(String[] args) {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        // url为调用webService的wsdl地址
        org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:9907/services/CodebtIndex?wsdl");
        // namespace是命名空间,methodName是方法名
        QName name = new QName("http://service.cib.com", "codebtIndex");
        // paramvalue为参数值
        String xmlStr = "612323199508163323";
        Object[] objects;
        try {
        objects = client.invoke(name, xmlStr);
        System.out.println(objects[0].toString());
        } catch (Exception e) {
        e.printStackTrace();
        }
   /*
   // 创建WebService客户端代理工厂
   JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
   // 判断是否抛出异常
   factory.getOutInterceptors().add(new LoggingInInterceptor());
   // 注册webservice接口
   factory.setServiceClass(DeductionService.class);
   // 配置webservice地址
   factory.setAddress("http://localhost:9907/services/CodebtIndex?wsdl");
   // 获得接口对象
   CxfService service = (CxfService) factory.create();
   // 调用接口方法
   String result = service.sayHello("612323199508163323");
   System.out.println("调用结果:" + result);
   // 关闭接口连接
   System.exit(0);
   */
        }

 

 

 

 

01_websevice接口示例

原文:https://www.cnblogs.com/loveduoduo/p/15028118.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!