<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>
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;
}
}
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);
}
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" ;
}
}
<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>
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);
*/
}
原文:https://www.cnblogs.com/loveduoduo/p/15028118.html