添加依赖
1 | <parent> |
1 | <dependency> |
webservice 服务端
- 接口
1 | "CommonService", // 暴露服务名称 (name = |
实现类
1
2
3
4
5
6
7
8
9
10
11
12
13"CommonService", // 与接口中指定的name一致 (serviceName =
targetNamespace = "http://service.webservice.com/", // 与接口中的命名空间一致,一般是接口的包名倒
endpointInterface = "com.webservice.CommonService"// 接口地址
)
public class CommonServiceImp implements CommonService {
public String sayHello(String name) {
return "Hello ," + name;
}
}配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class CxfConfig {
CommonService commonService;
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/services/*");
}
(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
/**
* Publishes this endpoint at the given address
**/
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), commonService);
endpoint.publish("/CommonService");
return endpoint;
}
}启动webservice 服务端
1
2
3
4
5
6
7
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}访问webservice服务
webservice 客户端调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49private static String addr="http://127.0.0.1:9000/dwjk/services/CommonService?wsdl";
private static String methodName = "sayHello";
private static String namespace="http://service.webservice.com/"
public void sayHello() {
String param = "liuwang";
CxfClient.invoke(endpointAddr,namespace,methodName,param);
}
/**
* @param wsURL
* @param namespace
* @param method
* @param params
*/
public static Object[] invoke(String wsURL, String namespace, String method,
Object... params) {
Object[] result;
long start = System.currentTimeMillis();
logger.info("【计时】webservice被调用,URL:" + String.valueOf(wsURL));
ClassLoader cl = Thread.currentThread().getContextClassLoader();
JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory
.newInstance();
try {
logger.info("【计时】webservice准备完成 "
+ (System.currentTimeMillis() - start));
Client client = clientFactory.createClient(wsURL);
//设置超时单位为毫秒
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(wsConnectionTimeout); //连接超时
httpClientPolicy.setAllowChunking(false); //取消块编码
httpClientPolicy.setReceiveTimeout(receiveTimeout); //响应超时
http.setClient(httpClientPolicy);
if (StringUtils.isNotBlank(namespace)) {
QName qName = new QName(namespace, method);
return result = client.invoke(qName, params);
} else {
return result = client.invoke(method, params);
}
} catch (Exception e) {
logger.error("调用失败" + "接口:" + wsURL + "方法:" + method);
throw new RuntimeException("调用webService获取结果出错", e);
} finally {
Thread.currentThread().setContextClassLoader(cl);
logger.info("【计时】webservice结束 "
+ (System.currentTimeMillis() - start));
}
}
拦截器
编写中。。。。