bte365官网

java发布webservice服务和动态调用webservice服务样例

发布时间: 2025-08-15 02:32:27 作者: admin 阅读量: 7714 评论数: 918

前言

在实际过程中,我们很多时候需要远程访问第三方程序提供的web服务或者需要主动提供一个web服务供第三方系统调用,从而达到异构系统之间的通信,保证数据进行了有效的交换。

一、webservice服务是什么?

Web服务是一种可以用来解决跨网络应用集成问题的开发模式,目的是保证不同平台的应用服务可以互操作。那么简单点,我们需要知道什么?

1、SOAP:远程过程调用的轻量级协议,是一个基于XML的协议,允许任何类型的对象或代码,在任何平台上,以任何一种语言相互通信。

2、WSDL:用来描述Web服务与Web服务通信的XML语言。告诉用户应该如何去调用这个服务的详细信息。

对于WebService调用而言,我们需要知道调用的几个核心参数 (调用地址、命名空间、调用方法、入参及类型、出参及类型…)

调用的地址 如:http://localhost:8080/esb/services/ts?wsdl

或者 http://localhost:8080/esb/services/ts(需要在后拼接上 ?wsdl 或者 .cls?wsdl )

因发布的方式,可能存在细微区别

命名空间 如下图所示

调用的方法及入参出参 可能存在多个,如下图,存在两个 分别为 getCharge,getCharge1

二、使用步骤

1.引入依赖(生成服务 和 调用服务 均使用此依赖)

代码如下

org.springframework.boot

spring-boot-starter-web

org.apache.cxf

cxf-rt-frontend-jaxws

3.1.12

org.apache.cxf

cxf-rt-transports-http

3.1.12

wsdl4j

wsdl4j

生成服务(注意如下是多个文件,我粘贴在同一个代码片段里面了)

/////////////实体类

package cn.tx.webservice.demoservice.pojo;

public class User {

private String name;

private String age;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAge() {

return age;

}

public void setAge(String age) {

this.age = age;

}

}

/////////////////////////////接口

package cn.tx.webservice.demoservice;

import cn.tx.webservice.demoservice.pojo.User;

import javax.jws.WebService;

@WebService(name = "ChargeWs", // 暴露服务名称

targetNamespace = "www.jiac.com"// 命名空间

)

public interface WS {

//标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法

User getCharge(String msg);

String getCharge1(String msg);

}

//////////////////实现接口

package cn.tx.webservice.demoservice.impl;

import cn.tx.webservice.demoservice.WS;

import cn.tx.webservice.demoservice.pojo.User;

import org.springframework.stereotype.Component;

import javax.jws.WebService;

/**

* @ClassName GetChargeImpl

* @Description TODO

* @Author jiac

* @Date 2021/11/10 14:19

* @Version 1.0

**/

@WebService(serviceName = "ChargeWs",

targetNamespace = "www.jiac.com", //与接口中的targetNamespace一致

endpointInterface = "cn.tx.webservice.demoservice.WS")

@Component

public class GetChargeImpl implements WS {

@Override

public User getCharge(String msg) {

User user =new User();

user.setName("jiac");

user.setAge("26");

return user;

}

@Override

public String getCharge1(String msg) {

return "收到信息";

}

}

/////////////////////////配置类

package cn.tx.webservice.demoservice.config;

import cn.tx.webservice.demoservice.WS;

import cn.tx.webservice.demoservice.impl.GetChargeImpl;

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;

/**

* @ClassName CxfConfig

* @Description TODO

* @Author jiac

* @Date 2021/11/10 14:23

* @Version 1.0

**/

@Configuration

public class CxfConfig {

@Bean("dispatcherServletCxf")

public ServletRegistrationBean wsDispatcherServlet(){

return new ServletRegistrationBean(new CXFServlet(),"/esb/services/*");//发布服务名称

}

@Bean(name = Bus.DEFAULT_BUS_ID)

public SpringBus springBus() {

return new SpringBus();

}

@Bean

public WS webService() {

return new GetChargeImpl();

}

//终端路径

@Bean

public Endpoint endpoint() {

EndpointImpl endpoint = new EndpointImpl(springBus(),webService());

endpoint.publish("/ts");

return endpoint;

}

@Bean

public Endpoint endpoint1() {

EndpointImpl endpoint = new EndpointImpl(springBus(),webService());

endpoint.publish("/tst");

return endpoint;

}

}

2.调用服务

代码如下:

package cn.tx.webservice;

import org.apache.cxf.endpoint.Client;

import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import javax.xml.namespace.QName;

import java.io.BufferedReader;

import java.io.FileReader;

/**

* @ClassName TestCXF

* @Description TODO

* @Author jiac

* @Date 2021/11/10 21:21

* @Version 1.0

**/

public class TestCXF {

public static void main(String[] args) throws Exception {

StringBuffer sbf = new StringBuffer();

BufferedReader bufferedReader=new BufferedReader(new FileReader("D:\\MAVEN\\tx_boot\\springboot_first\\src\\AdviceInfomationAdd.xml"));

String xml="";

while ( (xml=(bufferedReader.readLine()))!=null){

sbf.append(xml);

}

JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();

Client client = dcf.createClient("http://localhost:8080/esb/services/ts?wsdl");

System.out.println(sbf);

QName qName=new QName("www.jiac.com","getCharge1");

long t1 = System.currentTimeMillis();

Object[] invoke =client.invoke(qName,sbf.toString());

long t2 = System.currentTimeMillis();

String string = invoke[0].toString();

System.out.println(string);

System.out.println(t2-t1); //计算下通讯时长

}

}

效果如下

总结

大体总结一下记录下我学习的基础过程,希望对你也有所帮助。主要理解下webservice的相关含义,了解下生成服务时和调用服务时所需要的依赖包,生成服务时需要加载下配置类,及相关属性,并且需要了解下生成服务后wsdl的文件描述,通常文件描述是以下往上阅读,或者直接通过测试工具(soapui)加载下地址,动态调用时,需要指定调用服务地址、命名空间、及具体方法。

相关文章