项目中使用的是SpringBoot整合webservce (spring整合cxf需添加cxf-rt-frontend-jaxws,cxf-rt-transports-http依赖) (建议使用)...

原文链接: http://www.cnblogs.com/lixiangang/p/11536249.html

添加依赖


xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0

com.wyj
wyj-interface-service
0.0.1-SNAPSHOT
jar

wyj-interface-service
Demo project for Spring Boot


org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE


UTF-8
UTF-8
1.8



org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-test
test



org.apache.httpcomponents
httpclient
4.5.4


com.alibaba
fastjson
1.1.41



org.springframework.boot
spring-boot-devtools
true



org.apache.cxf
cxf-spring-boot-starter-jaxws
3.1.11


org.scala-lang
scala-library
2.11.0





org.springframework.boot
spring-boot-maven-plugin





 

服务端接口
package com.wyj.webservice;

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

/**
* webservice测试接口
*
*
* @author:WangYuanJun
* @date:2017年12月19日 下午9:36:49
*/
@WebService(name = "TestService", // 暴露服务名称
targetNamespace = "http://service.wyj.com"// 命名空间,一般是接口的包名倒序
)
public interface TestService {

@WebMethod
@WebResult(name = "String", targetNamespace = "")
String sendMessage(@WebParam(name = "username") String username);
}
 

 

服务端接口实现
package com.wyj.webservice;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

/**
* webservice测试接口实现
*
*
* @author:WangYuanJun
* @date:2017年12月19日 下午9:37:20
*/
@WebService(serviceName = "TestService", // 与接口中指定的name一致
targetNamespace = "http://service.wyj.com", // 与接口中的命名空间一致,一般是接口的包名倒
endpointInterface = "com.wyj.webservice.TestService"// 接口地址
)
@Component
public class TestServiceImpl implements TestService {

@Override
public String sendMessage(String username) {

return "hello "+username;
}

}
 

 

 

 

cxf配置
package com.wyj.webservice;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* cxf配置
*
*
* @author:WangYuanJun
* @date:2017年12月19日 下午9:38:24
*/
@Configuration
public class CxfConfig {

@Autowired
private Bus bus;

@Autowired
private TestService testService;

@Bean
public Endpoint endpoint(){
EndpointImpl endpoint = new EndpointImpl(bus, testService);
endpoint.publish("/TestService");
return endpoint;
}
}
 

 

默认服务在Host:port/services/*路径下 
将TestService接口发布在了路径/services/TestService下,wsdl文档路径为,http://localhost:8080/services/TestService?wsdl

 

 

基于cxf的客户端调用webservice接口
package webservice;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.junit.Test;

/**
* webservice客户端
*
*
* @author:WangYuanJun
* @date:2017年12月19日 下午9:39:49
*/
public class WebServiceTest {

@Test
public void testSend1(){

// 创建动态客户端
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/services/TestService?wsdl");

// 需要密码的情况需要加上用户名和密码
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
Object[] objects = new Object[0];
try {

// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("sendMessage", "wyj");
System.out.println("返回数据:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}
————————————————
 

 

转载于:https://www.cnblogs.com/lixiangang/p/11536249.html

你可能感兴趣的:(项目中使用的是SpringBoot整合webservce (spring整合cxf需添加cxf-rt-frontend-jaxws,cxf-rt-transports-http依赖) (建议使用)...)