开发工具:eclipse、jdk1.8、tomcat7.0
WebService服务端代码:
package com.gsl.service;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
/**
*
* <br>
* <b>说明:</b> 测试WebService
* <b>作者:</b> Guo.shiLin
* <b>日期:</b> 2018年8月11日
* @version 1.0
*/
@WebService
public class SayHello {
//给方法是客户端调用的方法,方法名自定义。
public String say(String name) {
return "Hello" + name + ",This is sayHelloWebService";
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/Service/SayHello", new SayHello());
System.out.println("service success!");
}
}
1、wsdl方式连接Webservice
先用命令去查看jdk bin里面有没有wsimport这个命令程序,只要在JDK1.6以上版本都会有。
输入命令:wsimport -s D:\tts9\workspace\WebServer\src -p com.gsl.client -keep http://localhost:8080/Service/SayHello?wsdl
生成类如下:
连接WebService测试类:
package com.gsl.test;
import com.gsl.client.SayHello;
import com.gsl.client.SayHelloService;
/**
* <br>
* <b>说明:</b> webService客户端测试类
* <b>作者:</b> Guo.shiLin
* <b>日期:</b> 2018年8月12日
* @version 1.0
*/
public class WebServiceTest {
public static void main(String[] args) {
//wsdl
SayHelloService shs = new SayHelloService();
SayHello sh = shs.getSayHelloPort();
String result = sh.say("gsl");
System.out.println("===result=="+result);
}
}
2、cxf动态客户端方式
maven依赖:
<!-- WebService cxf动态客户端jar依賴 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.10</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.2</version>
</dependency>
代码如下:
package com.generic.feng.common.utils;
import javax.xml.namespace.QName;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.generic.feng.modules.tbid.entity.TimersBidInfo;
import com.generic.feng.modules.tinvite.entity.TimersInviteInfo;
/**
* 接口请求工具类
*
* @author wim
* @version 1.0
* @since 2018年05月17日
*/
public class HttpConnectUtil {
private static Logger logger = LoggerFactory.getLogger(HttpConnectUtil.class);
//public static final String URL = "http://152.55.249.152:8181/ictpro/ws/receiveInfoWithSniffing?wsdl";
/**
* 招标接口输入参数,post请求
*
* @param json
* @return
*/
public static void doInvitePost(TimersInviteInfo timersInviteInfo) {
String s = "{"
+ "\"ROOT\":{"
+ "\"HEADER\":{"
+ "\"METHOD\":\"GET_TISKLIST\""
+ "},"
+ "\"BODY\":{"
+ "\"REQUEST_INFO\":[{"
+ "\"projectName\":\"" + timersInviteInfo.getInviteProjectName() + "\","
+ "\"tenderer\":\"" + timersInviteInfo.getInviteUnit() + "\","
+ "\"tendProjectCode\":\"" + timersInviteInfo.getInviteProjectNum() + "\","
+ "\"tendRegion\":\"" + timersInviteInfo.getRegionName() + "\","
+ "\"belongRegion\":\"" + timersInviteInfo.getCityName() + "\","
+ "\"publishDate\":\"" + timersInviteInfo.getInviteTime() + "\","
+ "\"closeDate\":\"" + timersInviteInfo.getDeadlineTime() + "\","
+ "\"startDate\":\"" + timersInviteInfo.getBidOpenTime() + "\","
+ "\"projectMoney\":\"" + timersInviteInfo.getProjectMoney() + "\","
+ "\"projectDesc\":\"" + timersInviteInfo.getProjectIntroduction() + "\","
+ "\"tendWeb\":\"" + timersInviteInfo.getUrlName() + "\","
+ "\"tendIp\":\"" + timersInviteInfo.getInviteProjectUrl() + "\""
+ "}]"
+ "}"
+ "}"
+ "}";
logger.debug("招标Json数据:" + s);
//cxf动态客户端方式
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://152.55.249.152:8181/ictpro/ws/receiveInfoWithSniffing?wsdl");
//Client client = dcf.createClient("http://10.153.195.92:9080/ictpro/ws/receiveInfoWithSniffing?wsdl");
QName qName = new QName("http://ws.webservices.friendone.com/", "getSniffingInfo");
try {
Object[] bObjects = client.invoke(qName, s);
logger.debug("InviteResultReponse:" + bObjects[0]);
} catch (Exception e) {
e.printStackTrace();
}
//wsdl方式
/*ReceiveInfoWithSniffingImp impl = new ReceiveInfoWithSniffingImp();
ReceiveInfoWithSniffing receiveInfoWithSniffing = impl.getReceiveInfoWithSniffingImpPort();
Object object = receiveInfoWithSniffing.getSniffingInfo2(param.toString());
System.out.println("object:" + object);*/
//getSniffingInfo.setArg0(param.toString());
//http接口方式
/* HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
JSONObject response = null;
try {
StringEntity s = new StringEntity(param.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
HttpResponse res = client.execute(post);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = res.getEntity();
String result = EntityUtils.toString(entity);// 返回json格式:
response = JSONObject.fromObject(result);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;*/
}
/**
* 中标接口输入参数,post请求
*
* @param json
* @return
*/
public static void doBidPost(TimersBidInfo timersBidInfo) {
// JSONObject param1 = new JSONObject();
// //GetSniffingInfo getSniffingInfo = new GetSniffingInfo();
// param1.put("METHOD", "GET_TISKLIST");
// JSONObject param2 = new JSONObject();
// param2.put("projectName", timersBidInfo.getBidProjectName());
// param2.put("tenderer", timersBidInfo.getBiddingUnit());
// param2.put("winProjectCode", timersBidInfo.getBidProjectNum());
// param2.put("tendRegion", timersBidInfo.getRegionName());
// param2.put("belongRegion", timersBidInfo.getCityName());
// param2.put("winPublishDate", timersBidInfo.getBidTime());
// param2.put("winUnit", timersBidInfo.getBidUnit());
// param2.put("winProjectMoney", timersBidInfo.getBidProjectMoney());
// param2.put("winProjectDesc", timersBidInfo.getProjectIntroduction());
// param2.put("winWeb", timersBidInfo.getUrlName());
// param2.put("winIp", timersBidInfo.getBidProjectUrl());
// JSONObject params = new JSONObject();
// params.put("HEADER", param1);
// JSONObject params2 = new JSONObject();
// params2.put("REQUEST_INFO", param2);
// params.put("BODY", params2);
// JSONObject param = new JSONObject();
// param.put("ROOT", params);
// System.out.println("param:" + param);
String s = "{"
+ "\"ROOT\":{"
+ "\"HEADER\":{"
+ "\"METHOD\":\"GET_TISKLIST\""
+ "},"
+ "\"BODY\":{"
+ "\"REQUEST_INFO\":[{"
+ "\"projectName\":\" " + timersBidInfo.getBidProjectName() + "\","
+ "\"tenderer\":\"" + timersBidInfo.getBiddingUnit() + "\","
+ "\"winProjectCode\":\"" + timersBidInfo.getBidProjectNum() + "\","
+ "\"tendRegion\":\"" + timersBidInfo.getRegionName() + "\","
+ "\"belongRegion\":\"" + timersBidInfo.getCityName() + "\","
+ "\"winPublishDate\":\"" + timersBidInfo.getBidTime() + "\","
+ "\"winUnit\":\"" + timersBidInfo.getBidUnit() + "\","
+ "\"winProjectMoney\":\"" + timersBidInfo.getBidProjectMoney() + "\","
+ "\"winProjectDesc\":\"" + timersBidInfo.getProjectIntroduction() + "\","
+ "\"winWeb\":\"" + timersBidInfo.getUrlName() + "\","
+ "\"winIp\":\"" + timersBidInfo.getBidProjectUrl() + "\""
+ "}]"
+ "}"
+ "}"
+ "}";
logger.debug("中标Json数据:" + s);
//cxf动态客户端方式
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://152.55.249.152:8181/ictpro/ws/receiveInfoWithSniffing?wsdl");
//Client client = dcf.createClient("http://10.153.195.92:9080/ictpro/ws/receiveInfoWithSniffing?wsdl");
QName qName = new QName("http://ws.webservices.friendone.com/", "getSniffingInfo2");
try {
Object[] bObjects = client.invoke(qName, s);
logger.debug("BidResultReponse:" + bObjects[0]);
} catch (Exception e) {
e.printStackTrace();
}
//wsdl方式
/* ReceiveInfoWithSniffingImp imp = new ReceiveInfoWithSniffingImp();
ReceiveInfoWithSniffing receiveInfoWithSniffing = imp.getReceiveInfoWithSniffingImpPort();
Object object = receiveInfoWithSniffing.getSniffingInfo2(param.toString());
System.out.println("object:" + object);*/
//http接口
/* HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
JSONObject response = null;
try {
StringEntity s = new StringEntity(param.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
HttpResponse res = client.execute(post);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = res.getEntity();
String result = EntityUtils.toString(entity);// 返回json格式:
response = JSONObject.fromObject(result);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;*/
}
//调用
public static void main(String arg[]) throws Exception {
System.out.println("============================================================");
//post 请求
TimersInviteInfo timersInviteInfo = null;
doInvitePost( timersInviteInfo);
}
}