本例使用cxf框架实现webservice
直接上代码:
maven依赖:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.3.0</version>
</dependency>
首先是服务端:
本例使用的springboot版本为2.0.3,较高版本的springboot启动会出现以下异常:
Bean method 'dispatcherServletRegistration' in 'DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration' not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet
一个接口:
package com.example.demo.webservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(name = "CxfService",targetNamespace = "http://webservice.demo.example.com")
public interface CxfService {
@WebMethod
@WebResult(name = "String",targetNamespace = "")
public String sayHello(@WebParam(name = "username") String username);
}
一个实现类:
package com.example.demo.webservice;
import javax.jws.WebService;
/**
* 功能描述:
*
* @author liuchaoyong
* @version 1.0
* @date 2019-03-03 16:12
*/
@WebService(serviceName = "CxfService",targetNamespace = "http://webservice.demo.example.com",endpointInterface = "com.example.demo.webservice.CxfService")
public class CxfServiceImpl implements CxfService{
@Override
public String sayHello(String username) {
return "hello:" + username;
}
}
一个配置类:
package com.example.demo.webservice;
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;
/**
* 功能描述:
*
* @author liuchaoyong
* @version 1.0
* @date 2019-03-03 16:15
*/
@Configuration
public class CxfConfig {
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/service/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public CxfService cxfService(){
return new CxfServiceImpl();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), cxfService());
endpoint.publish("/say");
return endpoint;
}
}
然后我们启动项目,访问该路径:http://localhost:8080/service,你的路径就是你的ip+端口+配置类中设置的ServletRegistrationBean的第二个参数,这个路径下放了你所有对外提供的服务
效果如图:
点进wdsl链接,显示的就是webservice的xml:
然后是客户端:
根据服务端暴露的wsdl,使用命令行生成客户端,包含.java文件
wsimport -s 客户端所在的项目路径 wsdl的url
或者你也可以随便生成到某个地方,然后把.java文件全部粘过去
生成过程:
生成的代码:
服务端调用:
package com.example.demo.client;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 功能描述:
*
* @author liuchaoyong
* @version 1.0
* @date 2019-03-04 09:49
*/
@RestController
@RequestMapping("/test")
public class TestApi {
@GetMapping("say")
public Object say(String username){
CxfService_Service cxfServiceService = new CxfService_Service();
return cxfServiceService.getCxfServiceImplPort().sayHello(username);
}
}