上一节我们讲述了springboot整合webservice,这节我们讲述自动生成webservice客户端。
1.首先配置service和实现类
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
-
webService 接口
-
@Date: 2019-01-08 13:47
-
@Version 1.0
*/
@WebService(name = “CommonService”, // 暴露服务名称
targetNamespace = “http://service.web.tech.yankuan.com”) //命名空间,一般是接口的包名倒序
public interface CommonService {@WebMethod
@WebResult(name = “String”,targetNamespace = “”)
public String helloWorld(@WebParam(name = “HelloName”) String name);@WebMethod
@WebResult(name = “String”,targetNamespace = “”)
public String sayHello(@WebParam(name = “HelloName”) String name);
}
实现类:
import com.yankuang.technology.web.service.CommonService;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
/**
-
@Date: 2019-01-08 13:54
-
@Version 1.0
*/
@WebService(serviceName = “CommonService”,//与前面接口一致
targetNamespace = “http://service.web.tech.yankuan.com”, //与前面接口一致
endpointInterface = “com.yankuan.tech.web.service.CommonService”) //接口地址
@Component
public class CommonServiceImpl implements CommonService {
@Override
public String helloWorld(String name) {
return “你好 —>”+name;
}@Override
public String sayHello(String name) {
return “你好 —>”+name;
}
}
发布接口:
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 javax.xml.ws.Endpoint;
/**
-
@Date: 2019-01-08 13:58
-
@Version 1.0
-
webservice
*/
@Configuration
public class WebConfig {@Autowired
private Bus bus;@Autowired
CommonService service;@Autowired
AchieveExpressService achieveExpressService;@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, service);
endpoint.publish("/CommonService");
return endpoint;
}@Bean
public Endpoint endpoint2() {
EndpointImpl endpoint = new EndpointImpl(bus, achieveExpressService);
endpoint.publish("/AchieveExpressService");
return endpoint;
}
}
启动项目,验证是否接口发布成功
浏览器中输入接口地址:http://192.168.1.135:12301/services/CommonService?wsdl
如果出现以下界面说明发布成功
下边是发布的地址:
下边是Idea自带webservice 客户端生成器:
右键点击项目,找到webservice 如图
接下来填写相关信息:
点击ok后生成如下文件:
生成的class文件可以删除
编写main方法调用:
import com.alibaba.fastjson.JSONObject;
import java.util.List;
/**
-
@Date: 2019-01-19 12:38
-
@Version 1.0
*/
public class DemoClent {public static void main(String[] args) {
c2();
}
// public static void c1(){
// WebCXFService_Service service_service = new WebCXFService_Service();
// WebCXFService webCXFService = service_service.getWebServiceImplPort();
// String s = webCXFService.hello(“你好”);
// System.out.println(s);
// }
//
public static void c2(){
CommonService_Service service_service = new CommonService_Service();
CommonService webCXFService = service_service.getCommonServiceImplPort();
String s = webCXFService.sayHello(“青岛”);
System.out.println(s);
}
}
调用成功