发布webservice中所有非静态的方法都会被发布,final和静态方法不会对外发布。
自定义webservice服务端
1.首先引入maven依赖
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-frontend-jaxwsartifactId>
<version>3.1.6version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-transports-httpartifactId>
<version>3.1.6version>
dependency>
2.定义Cxf配置类
@Configuration
public class SapCxfConfig {
//第二个参数url跟在服务器端口后面
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/webservce/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public ISapPushExpenseWebservice sapPushExpenseWebservice() {
return new SapPushExpenseWebservice();
}
//sapPushExpenseWebservice你要对外开放的对象
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), sapPushExpenseWebservice());
//publish()进行对外发布,里面的api跟在上面webservice参数的后面
endpoint.publish("/api");
return endpoint;
}
}]
service类
@WebService(name = "SapPushExpenseWebservice")
public interface ISapPushExpenseWebservice {
@WebMethod(operationName="sayHello") //相当于为这个方法制定别名,在wsdl节点上就会显示该方法名称为sayHello
@WebMethod(exclude=true) //设置为true的情况下,该方法不会对外进行发布
@WebResult(name="address") //表明方法的返回值名称,在发布wsdl中可以看到
//@WebParam(name="address")该注解表示给这个参数定义名称,否则wsdl默认显示arg0
String pushExpense(@WebParam(name="address")String address);
}
@WebService(name = "SapPushExpenseWebservice")
@Service
public class SapPushExpenseWebservice implements ISapPushExpenseWebservice {
private static Logger logger = LoggerFactory.getLogger(SapPushExpenseWebservice.class);
@Override
public String pushExpense(String address) {
logger.info(JSON.toJSONString(address));
return address;
}
}
上述注解详情参考这位博主的解说:
https://blog.csdn.net/cherry_11qianqian/article/details/82662315
https://blog.csdn.net/cherry_11qianqian/article/details/81536727
3.启动项目就可以看到去访问了,我是本地访问http://localhost:8010/webservice
address:显示我本地服务端的地址
wsdl:显示我这个服务端开放对象的文件夹,它这个是反过来的
namespace:显示的对象的文件夹
打开页面可以看到下面有wsdl的地址,点击跳转即可!!!
portType节点展示的是你对外开放的对象和方法名称
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://impl.service.webservice_server.gongj.com/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="UserServerImplService"
targetNamespace="http://impl.service.webservice_server.gongj.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://impl.service.webservice_server.gongj.com/" elementFormDefault="unqualified" targetNamespace="http://impl.service.webservice_server.gongj.com/" version="1.0">
<xs:element name="getUser" type="tns:getUser"/>
<xs:element name="getUserResponse" type="tns:getUserResponse"/>
<xs:complexType name="getUser">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:long"/>
xs:sequence>
xs:complexType>
<xs:complexType name="getUserResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="tns:userDTO"/>
xs:sequence>
xs:complexType>
<xs:complexType name="userDTO">
<xs:sequence>
<xs:element minOccurs="0" name="address" type="xs:string"/>
<xs:element minOccurs="0" name="age" type="xs:int"/>
<xs:element minOccurs="0" name="id" type="xs:long"/>
<xs:element minOccurs="0" name="name" type="xs:string"/>
xs:sequence>
xs:complexType>
xs:schema>
wsdl:types>
<wsdl:message name="getUser">
<wsdl:part element="tns:getUser" name="parameters">
wsdl:part>
wsdl:message>
<wsdl:message name="getUserResponse">
<wsdl:part element="tns:getUserResponse" name="parameters">
wsdl:part>
wsdl:message>
<wsdl:portType name="UserServerImpl">
<wsdl:operation name="getUser">
<wsdl:input message="tns:getUser" name="getUser">
wsdl:input>
<wsdl:output message="tns:getUserResponse" name="getUserResponse">
wsdl:output>
wsdl:operation>
wsdl:portType>
<wsdl:binding name="UserServerImplServiceSoapBinding" type="tns:UserServerImpl">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getUser">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getUser">
<soap:body use="literal"/>
wsdl:input>
<wsdl:output name="getUserResponse">
<soap:body use="literal"/>
wsdl:output>
wsdl:operation>
wsdl:binding>
<wsdl:service name="UserServerImplService">
<wsdl:port binding="tns:UserServerImplServiceSoapBinding" name="UserServerImplPort">
<soap:address location="http://localhost:8080/webservice/api"/>
wsdl:port>
wsdl:service>
wsdl:definitions>
webservice客户端
首先引入依赖:这个依赖跟上面服务端依赖一致哦!!!
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-frontend-jaxwsartifactId>
<version>3.1.9version>
dependency>
<dependency>
<groupId>org.apache.cxfgroupId>
<artifactId>cxf-rt-transports-httpartifactId>
<version>3.1.9version>
dependency>
1.简单的写法
private static Logger logger= LoggerFactory.getLogger(DemoApplication.class);
public static void main(String[] args){
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8010/webservice/api?wsdl");
try {
// invoke("调用服务端某个方法的方法名",传递的参数1,参数2,参数3....);
Object[] objects = client.invoke("pushExpense", "上海市");
logger.info("输出内容:"+objects);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
//输出:输出内容:上海市
2.可以把对象进行简单封装
①属性类
@Data
public class WebClientConfig {
//路径url
private String url;
//对应要调用的方法名
private String method;
//设置是否开启这个对象的调用
private boolean enabled;
}
②客户端调用的公共配置类
@ConfigurationProperties(prefix = "test-web-service")
@Component
public class CustomWebServiceClient {
private Logger logger= LoggerFactory.getLogger(CustomWebServiceClient.class);
public void setConfigs(Map<String, WebClientConfig> configs) {
this.configs = configs;
}
//存储读取配置文件的数据对象
private Map<String,WebClientConfig> configs;
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
public Object[] sendAdnReceiveInfo(String configId,Object... data){
//根据每个不同的调用对象传递调用参数,获取url等参数
WebClientConfig clientConfig=configs.get(configId);
if(Objects.isNull(clientConfig)||!clientConfig.isEnabled()){
logger.info("webservice连接对象为null,或者连接配置未开启!");
return null;
}
Object[] obj;
String wsdUrl=clientConfig.getUrl();
try {
//获取cxf客户端
Client client=dcf.createClient(wsdUrl);
//第一个参数是被调用方的方法名,后面参数是传递过去的值
obj=client.invoke(clientConfig.getMethod(),data);
System.out.println("输出内容:"+obj);
}catch (Exception e){
throw new RuntimeException(e);
}finally {
logger.info("关闭了!!!");
}
return obj;
}
}
③xml配置
test-web-service:
configs:
TEST_WEB:
enabled: true
url: "http://localhost:8010/webservice/api?wsdl"
method: "pushExpense"
③调用方法
@Autowirde
private CustomWebServiceClient customWebServiceClient;
public String send() {
//第一个参数为调用配置文件的那个对象key,第二个参数就是传递的值啦,也可以传多个值,就完成啦!!!
String address= customWebServiceClient.sendAdnReceiveInfo("TEST_WEB","上海市");
sout(address);
}