第一步:创建一个springboot项目并启动 https://mp.csdn.net/mdeditor/89018104#
第二步:引入jar包
<!-- CXF webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.1.7</version>
</dependency>
<!-- CXF webservice -->
第三步:书写接口类和实现类以及发布配置
如图所示:
接口 CommonService
package wang.test.web;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(name = "CommonService", // 暴露服务名称
targetNamespace = "http://webservice.leftso.com/"// 命名空间,一般是接口的包名倒序
)
public interface CommonService {
@WebMethod
@WebResult(name = "String", targetNamespace = "")
public String sayHello(@WebParam(name = "userName") String name);
}
实现类CommonServiceImp
package wang.test.web;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
/**
* 接口实现
*
* @author leftso
*
*/
@WebService(serviceName = "CommonService", // 与接口中指定的name一致
targetNamespace = "http://web.test.wang/", // 与接口中的命名空间一致,一般是接口的包名倒
endpointInterface = "wang.test.web.CommonService"// 接口地址
)
@Component
public class CommonServiceImp implements CommonService {
@Override
public String sayHello(String name) {
return "Hello ," + name;
}
}
发布配置CxfConfig
package wang.test.config;
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;
import wang.test.web.CommonService;
@Configuration
public class CxfConfig {
@Autowired
private Bus bus;
@Autowired
CommonService commonService;
/** JAX-WS **/
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, commonService);
endpoint.publish("/CommonService");
return endpoint;
}
}
第四步:启动服务并访问生成xml 出现下图即为成功
问题1 Cxf开发webservice时遇到Can’t find the request for xx’s Observer
我这边的错误是springboot版本不兼容的问题 我之前用的是1.5.4.RELEASE 后改为1.4.5.RELEASE
就可以了
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
: ) 补充-----------------------------------------------------------------------------------------------------------
客户端
步骤一:在另一个项目中 右键—新建—Web Service Client—下一步
步骤二:将服务端发布的wsdl路径添加到Service definition 然后完成
生成文件如下:
步骤三:新建一个类
import java.rmi.RemoteException;
public class EditSrceeningTest {
public static void main(String[] args) {
/* MobileCodeWS mc = new MobileCodeWS();
MobileCodeWSSoap soap = mc.getMobileCodeWSSoap();
String str = soap.getMobileCodeInfo("13011286707", null);
System.out.println(str);*/
/**
* 利用axis调用webservice实例
* @param args
*/
try {
//换成对应的proxy类
EditScreeningProxy proxy = new EditScreeningProxy();
proxy.setEndpoint("http://10.11.23.76:8803/services/EditScreening?wsdl");
//换成获取对应的serice
EditScreening_PortType service = proxy.getEditScreening_PortType();
//调用web service提供的方法
String result = service.edit("客户端传输---");
System.out.println(result);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
如图:
启动main方法!获得返回数据成功!(备注: 我这里还有点问题 -_-)