springmvc是什么框架框架如何实现像webservice一样通过wsdl2Java生成客户端代码

2016年1月 Java大版内专家分月排行榜第二2015年12月 Java大版内专家分月排行榜第二2015年8月 Java大版内专家分月排行榜第二2015年3月 Java大版内专家分月排行榜第二2015年1月 Java大版内专家分月排行榜第二2014年12月 Java大版内专家分月排行榜第二2014年11月 Java大版内专家分月排行榜第二2014年6月 Java大版内专家分月排行榜第二2014年4月 Java大版内专家分月排行榜第二2014年1月 Java大版内专家分月排行榜第二2013年11月 Java大版内专家分月排行榜第二
2015年9月 Java大版内专家分月排行榜第三2015年6月 Java大版内专家分月排行榜第三2015年5月 Java大版内专家分月排行榜第三2015年2月 Java大版内专家分月排行榜第三2014年3月 Java大版内专家分月排行榜第三2013年12月 Java大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。博客分类:
附件中附带页面jsp、js,还有dwr的action,service以及util,我的环境是spring、dwr、ext、jquery。由于整个工具牵扯的比较多,所以没有将完整的可运行的代码整理出来,只将所有核心的代码贴了出来,如果需要运行还需要解决些小问题
近段时间,需要为公司的QA测试人员提供一个Webservice的测试工具,具体要求为:测试人员提供webservice的url,测试工具根据url得到webservice发布的方法及方法的参数,然后测试人员在页面输入参数,并点击运行,工具运行后,在页面上显示返回的结果。其实测试人员可以用现成在测试框架soapui来测试webservice,不过测试人员仍觉得麻烦,Team Leader还要求工具不能用wsdl2java生成webservice的客户端代码。我想可能是不想在项目中存放过多的无用的临时文件吧。
测试工具支持参数和返回值的类型有:数组、List、java基本数据类型、java对象以及这些类型的相互嵌套。工具截图如下
下面说说工具的实现思路:
用wsdl4j工具根据webservice的url解析出wsdl文件中包含的方法
利用dom解析wsdl的schema部分,手动解析出方法的参数
利用soapui获得发送soap请求消息的模板
将参数填入发送的soap请求消息
获得返回的soap消息
解析返回的soap消息中的返回值,显示到页面
在看实现代码前,不懂wsdl和schema的朋友们可以根据以下网址,先熟悉一下wsdl和schema文件的结构,和各个属性字段
wsdl:
schema:
代码讲解:
用于在页面显示方法和方法参数的实体bean
/**
* @author zhengtian
下午11:21:50
@SuppressWarnings("all")
public class WebServiceMethod {
public String getName() {
public void setName(String name) {
this.name =
import java.util.ArrayL
import java.util.L
public class ParameterInfo {
private S// 参数名
private S// 参数值
private S// 参数类型
private String childT// 如果是数组,那么该字段为数组元素的类型
private List&ParameterInfo& children = new ArrayList&ParameterInfo&();
public ParameterInfo() {
public ParameterInfo(String name, String type) {
this.name =
this.type =
public String getName() {
public void setName(String name) {
this.name =
public String getValue() {
public void setValue(String value) {
this.value =
public String getType() {
public void setType(String type) {
this.type =
public List&ParameterInfo& getChildren() {
public void setChildren(List&ParameterInfo& children) {
this.children =
public String getChildType() {
return childT
public void setChildType(String childType) {
this.childType = childT
* 增加子参数
* @param param
public void addChild(ParameterInfo param) {
children.add(param);
获取webservice发布的方法
public List&WebServiceMethod& getAllMethodByServiceUrl(String webserviceUrl) throws Exception {
List&WebServiceMethod& list = new ArrayList&WebServiceMethod&();
// 将url修正为合法的url,即带wsdl后缀的
webserviceUrl = getWebserviceUrl(webserviceUrl);
if (StringUtils.isNotEmpty(webserviceUrl)) {
List&String& methodList = WsdlUtil.getOperationList(webserviceUrl);
for (String methodName : methodList) {
WebServiceMethod webServiceMethod = new WebServiceMethod();
webServiceMethod.setName(methodName);
list.add(webServiceMethod);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
* 得到wsdl中所有的方法
* @param wsdlUrl
* @throws Exception
public static List&String& getOperationList(String wsdlUrl) throws Exception {
Document document = getDefinitionDocument(wsdlUrl);
XPath xpath = getXpath(document);
NodeList operations = DOMUtil.findNodeList(document, "wsdl:definitions/wsdl:portType/wsdl:operation");
// 返回的结果集list
List&String& operationList = new ArrayList&String&();
for (int i = 0; i & operations.getLength(); i++) {
Node operation = operations.item(i);
String operationName = DOMUtil.getNodeName(operation);
if (operationName != null && !"".equals(operationName)) {
log.debug("解析" + wsdlUrl + "中的方法:" + operationName);
operationList.add(operationName);
return operationL
得到方法的参数
* 根据方法名称和webserviceUrl得到参数
* @param methodName
* @param webserviceUrl
* @throws Exception
public List&ParameterInfo& getParamByMethodNameAndWsUrl(String methodName, String webserviceUrl) throws Exception {
Document document = WsdlUtil.getDefinitionDocument(webserviceUrl);
// 返回结果
List&ParameterInfo& inputParamList = new ArrayList&ParameterInfo&();
// 解析参数
StringBuilder xpathBuilder = new StringBuilder();
WsdlUtil.getInputParam(inputParamList, document, methodName, xpathBuilder, null, false);
return inputParamL
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
解析输入参数的核心方法
* 得到输入参数
* @param inputParamList
* @param document
* @param operationName
* @param xpathBuilder
* @param parentParam
* @param isSelfDefinition
* @throws Exception
public static void getInputParam(List&ParameterInfo& inputParamList, Document document, String operationName, StringBuilder xpathBuilder,
ParameterInfo parentParam, boolean isSelfDefinition) throws Exception {
// 得到complexTypeName
String complexTypeName = "";
if (parentParam == null) {
complexTypeName = operationN
if (parentParam.getType().equals(SchemaDefaulyType.type_array.type)) {
complexTypeName = parentParam.getChildType();
complexTypeName = parentParam.getType();
// 得到所有的element结点
List&Node& children = getSequenceElementOfComplexType(document, parentParam, complexTypeName, xpathBuilder, isSelfDefinition);
for (int i = 0; i & children.size(); i++) {
Node child = children.get(i);
String name = DOMUtil.getNodeName(child);
ParameterInfo param = new ParameterInfo();
param.setName(name);
// 是否存在type属性(判断其type是引用还是自身定义)
if (DOMUtil.assertNodeAttributeExist(child, "type")) {//type存在
String type = DOMUtil.getNodeType(child);
if (DOMUtil.isArray(child)) {
param.setType(SchemaDefaulyType.type_array.type);
param.setChildType(type);
// 如果是简单的数组,则为数组增加一个子参数
ParameterInfo childParam = new ParameterInfo("", type);
param.addChild(childParam);
// 复杂类型数组
if (!DOMUtil.isDefaultType(child)) {
getInputParam(inputParamList, document, operationName, xpathBuilder, childParam, false);
param.setType("anyType".equals(type) ? "object" : type);
// 复杂类型
if (!DOMUtil.isDefaultType(child)) {
StringBuilder complextXpath = new StringBuilder("wsdl:definitions/wsdl:types/xs:schema");
getInputParam(inputParamList, document, operationName, complextXpath, param, false);
} else {// 如果type属性不存在,说明该结点的类型在其子结点中定义
String currentAppendStr = "/xs:complexType[@name='" + parentParam.getType() + "']/xs:sequence/xs:element[@name='" + name + "']";
xpathBuilder.append(currentAppendStr);
Node inner = DOMUtil.findNode(document, xpathBuilder.toString() + "/xs:complexType/xs:sequence/xs:element[position()=1]");
if (DOMUtil.isArray(inner)) {
// 得到数组的类型
String type = getSequenceElementType(document, inner);
param.setType(SchemaDefaulyType.type_array.type);
param.setChildType(type);
// 为数组增加一个子参数
ParameterInfo childParam = new ParameterInfo("", type);
param.addChild(childParam);
if (!DOMUtil.isDefaultType(type)) {// 复杂类型数组
getInputParam(inputParamList, document, operationName, xpathBuilder, childParam, true);
param.setType(name);
// 遍历其子结点xs:element
getInputParam(inputParamList, document, operationName, xpathBuilder, param, true);
// 将xpath还原
xpathBuilder.delete(xpathBuilder.length() - currentAppendStr.length(), xpathBuilder.length());
if (parentParam == null) {
inputParamList.add(param);
parentParam.addChild(param);
当页面输入完参数后,执行方法
* 执行方法
* @param webserviceUrl
* @param methodName
* @param paramStr
* @throws Exception
public String executionMethod(String webserviceUrl, String methodName, String paramStr) throws Exception {
String result = "";
// 将json参数转换为List&ParameterInfo&
List&ParameterInfo& paramList = convertStrToListParam(paramStr);
List&ParameterInfo& resultList = new SoapUtil().sendRequest(methodName, paramList, webserviceUrl);
result = JSONArray.fromObject(resultList).toString();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
发送soap请求,然后获得返回的soap消息
* 发送请求
* @param operation
* @param params
* @param wsdlUrl
* @throws Exception
public List&ParameterInfo& sendRequest(String operation, List&ParameterInfo& paramList, String wsdlUrl) throws Exception {
// 获取操作
Operation operationInst = getOperation(wsdlUrl, operation, null);
// 组装请求消息
String message = buildRequest(wsdlUrl, operationInst, paramList);
// 发送请求,得到返回的soap消息
String address = wsdlUrl.substring(0, wsdlUrl.indexOf("?wsdl"));
String responseStr = sendRequest(address, message, operationInst.getAction());
Document soapDocument = getResponseDocument(responseStr);
// 判断返回的soap消息是否为soap:Fault
if (isFaultResponseSoap(soapDocument)) {
processFaultResponseSoap(soapDocument);
// 解析返回结果
List&ParameterInfo& outPutParamList = new ArrayList&ParameterInfo&();
List&Map&String, Object&& wsdlMapSoapList = new ArrayList&Map&String, Object&&();
String complextTypeName = operation + "Response";
getOutPutParam(WsdlUtil.getDefinitionDocument(wsdlUrl), soapDocument, complextTypeName, complextTypeName, wsdlMapSoapList, outPutParamList,
return outPutParamL
将参数解析成发送的soap消息核心方法
* 构建soap消息
* @param wsdlDocument
* @param soapDocument
* @param operationInst
* @param paramList
* @param parentNode
* @throws Exception
private void buildSOAPMessage(Document wsdlDocument, Document soapDocument, Operation operationInst, List&ParameterInfo& paramList,
Node parentNode, ParameterInfo parentParam) throws Exception {
// 操作名称
String operationName = operationInst.getName();
// 如果是操作方法的根节点,则清空其子节点
if (parentNode == null) {
parentNode = getOperationNodeInRequestSoapDom(soapDocument, operationName);
parentNode.setTextContent("");
for (int i = 0; i & paramList.size(); i++) {
// 得到参数的name、type、value
ParameterInfo param = paramList.get(i);
String value = param.getValue();
String name = param.getName();
String type = param.getType();
// 判断是否为基本类型
if (DOMUtil.isDefaultType(type) || (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(type))
|| ("entry".equals(type) || "JsonEntry".equals(type))) {
if (StringUtils.isEmpty(name)) {
if (i & 0) {
Element ele = soapDocument.createElement(parentNode.getNodeName());
Text text = soapDocument.createTextNode(value);
ele.appendChild(text);
Node grandParentNode = parentNode.getParentNode();
grandParentNode.appendChild(ele);
if ("entry".equals(type) || "JsonEntry".equals(type)) {
Element ele = soapDocument.createElement(type);
parentNode.appendChild(ele);
// 组装子结点
if (param.getChildren().size() & 0) {
List&Node& childList = DOMUtil.getChildElementNodes(parentNode);
Node lastChildNode = childList.get(childList.size() - 1);
buildSOAPMessage(wsdlDocument, soapDocument, operationInst, param.getChildren(), lastChildNode, param);
Text text = soapDocument.createTextNode(value);
parentNode.appendChild(text);
Element ele = soapDocument.createElement(name);
Text text = soapDocument.createTextNode(value);
ele.appendChild(text);
parentNode.appendChild(ele);
// 组装子结点
if (param.getChildren().size() & 0) {
List&Node& childList = DOMUtil.getChildElementNodes(parentNode);
Node lastChildNode = childList.get(childList.size() - 1);
buildSOAPMessage(wsdlDocument, soapDocument, operationInst, param.getChildren(), lastChildNode, param);
} else {// 如果不是基本类型,则直接组装该节点的子结点
if (i & 0) {
Element ele = soapDocument.createElement(parentNode.getNodeName());
Node grandParentNode = parentNode.getParentNode();
grandParentNode.appendChild(ele);
// 组装子结点
if (param.getChildren().size() & 0) {
List&Node& childList = DOMUtil.getChildElementNodes(grandParentNode);
Node lastChildNode = childList.get(childList.size() - 1);
buildSOAPMessage(wsdlDocument, soapDocument, operationInst, param.getChildren(), lastChildNode, param);
// 组装子结点
if (param.getChildren().size() & 0) {
buildSOAPMessage(wsdlDocument, soapDocument, operationInst, param.getChildren(), parentNode, param);
将返回的soap消息解析成页面参数核心方法
* 解析返回的soap消息,然后将结果填充到outPutParamList中
* @param wsdlDocument
* @param soapDocument
* @param operationResponseName
* @param complextTypeName
* @param wsdlMapSoapList
* @param outPutParamList
* @throws Exception
public void getOutPutParam(Document wsdlDocument, Document soapDocument, String operationResponseName, String complextTypeName,
List&Map&String, Object&& wsdlMapSoapList, List&ParameterInfo& outPutParamList, ParameterInfo parent) throws Exception {
// 得到返回的参数
List&Node& outPutNodeList = WsdlUtil.getSequenceElementOfComplexType(wsdlDocument, complextTypeName);
for (int i = 0; i & outPutNodeList.size(); i++) {
Map&String, Object& wsdlMapSoap = new HashMap&String, Object&();
// 组装参数param
Node outPutNode = outPutNodeList.get(i);
String name = DOMUtil.getNodeName(outPutNode);
String type = DOMUtil.getNodeType(outPutNode);
ParameterInfo currentParam = new ParameterInfo();
currentParam.setName(name);
if (DOMUtil.isDefaultType(outPutNode)) {// 该参数为基本类型
if (DOMUtil.isArray(outPutNode)) {// 数组
currentParam.setType(WsdlUtil.SchemaDefaulyType.type_array.getType());
currentParam.setChildType(type);
// 组装映射关系
wsdlMapSoap.put("name", name);
wsdlMapSoap.put("index", new Integer(-1));
wsdlMapSoapList.add(wsdlMapSoap);
// 得到该数组在返回soap消息中的个数
int arrayLength = getArrayLengthFromResponseSoap(soapDocument, operationResponseName, wsdlMapSoapList);
// 从soap消息中取出值
for (int j = 1; j &= arrayL j++) {
ParameterInfo param = new ParameterInfo();
param.setName(name);
param.setType(type);
wsdlMapSoap.put("index", new Integer(j));
String value = getValueFromResponseSoap(soapDocument, wsdlMapSoapList, operationResponseName);
param.setValue(value);
currentParam.addChild(param);
} else {// 不是数组
currentParam.setType(type);
// 根据映射关系,从返回的soap消息中取值
wsdlMapSoap.put("name", name);
wsdlMapSoap.put("index", new Integer(-1));
wsdlMapSoapList.add(wsdlMapSoap);
String value = getValueFromResponseSoap(soapDocument, wsdlMapSoapList, operationResponseName);
currentParam.setValue(value);
} else {// 该参数为复杂类型
if (DOMUtil.isArray(outPutNode)) {// 数组
currentParam.setType(WsdlUtil.SchemaDefaulyType.type_array.getType());
currentParam.setChildType(type);
// 组装映射关系
wsdlMapSoap.put("name", name);
wsdlMapSoap.put("index", new Integer(-1));
wsdlMapSoapList.add(wsdlMapSoap);
// 得到该数组在返回soap消息中的个数
int arrayLength = getArrayLengthFromResponseSoap(soapDocument, operationResponseName, wsdlMapSoapList);
// 从soap消息中取出值
for (int j = 1; j &= arrayL j++) {
ParameterInfo param = new ParameterInfo();
param.setType(type);
wsdlMapSoap.put("index", new Integer(j));
// 继续查找
getOutPutParam(wsdlDocument, soapDocument, operationResponseName, type, wsdlMapSoapList, outPutParamList, param);
currentParam.addChild(param);
} else {// 不是数组
currentParam.setType(type);
// 根据映射关系,从返回的soap消息中取值
wsdlMapSoap.put("name", name);
wsdlMapSoap.put("index", new Integer(-1));
wsdlMapSoapList.add(wsdlMapSoap);
// 继续查找
getOutPutParam(wsdlDocument, soapDocument, operationResponseName, type, wsdlMapSoapList, outPutParamList, currentParam);
// 增加参数
if (parent == null) {
outPutParamList.add(currentParam);
parent.addChild(currentParam);
// 在映射关系中除去当前的结点
wsdlMapSoapList.remove(wsdlMapSoapList.size() - 1);
下载次数: 1439
浏览 10976
貌似这个工具不是通用的啊 对有的WSD服务地址不能够解析出来,作者能够给个QQ交流一下吗
zheng12tian
浏览: 275050 次
来自: 杭州
不好用啊,查询失败
我是拿来现成用的,非常感谢博主的代码,不过我给和我一样的人一个 ...
不错,基于冯立彬的文章,然后再自己亲自尝试下,赞+,加油~
漂亮,说明和示例都很精要!
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'\ 史上最详cxf-Springmvc-maven实现webservice教程
史上最详cxf-Springmvc-maven实现webservice教程
JAVA开发工程师
情劫难逃。
作者的热门手记
虽知道webservice,工作两年一直没使用过,最近不忙趁机研究了下,实现了简单的服务端及客户端调用。鉴于慕课网没有webservice的教程,大多又都是学生,就在这里跟大家分享下,内容比较详细。大神请忽略,如有错误之处,敬请指点。
第一步,下载cxf及配置环境变量。
Source Distribution为源码版,需要编译后使用,鄙人小白,没有搞过这种东西。我们下载Binary Distribution(可执行版),下载后解压即可。
作为java常用工具,最好在环境变量里配置一下,另cxf-3.1.8需要jdk1.6以上
在系统变量里的Path中添加(注意两个变量之间要有分号)
在cmd中输入wsdl2java -help输出一大堆
证明配置成功
第二步,普通项目,java-api实现webservice
@WebService
public interface MyWebService {
int add(int a, int b);
int minus(int a, int b);
@WebService(endpointInterface = "webservice.webserviceJAX.server.MyWebService")
public class MyWebserviceImpl implements MyWebService {
public int add(int a, int b) {
System.out.println(a+"+"+b+"="+(a+b));
return a+b;
public int minus(int a, int b) {
System.out.println(a+"-"+b+"="+(a-b));
return a-b;
public class MyServer {
public static void main(String args[]) {
String address = "http://localhost:8888/ms";
Endpoint.publish(address, new MyWebserviceImpl());
错误信息:Cannot find any registered HttpDestinationFactory from the Bus.
解决办法:添加cxf的jetty支持
&dependency&
&groupId&org.apache.cxf&/groupId&
&artifactId&cxf-rt-transports-http-jetty&/artifactId&
&version&3.1.8&/version&
&/dependency&
这时,可以打开浏览器并输入http://localhost:8888/ms?wsdl,可以看到暴漏的wsdl
4.客户端调用
public class MyClient {
public static void main(String args[]) {
URL url = new URL("http://localhost:8888/ms?wsdl");
//命名空间 及 名称
QName qName = new QName("http://server.webserviceJAX.webservice/","MyWebserviceImplService");
Service service = Service.create(url, qName);
MyWebService myWebservice = service.getPort(MyWebService.class);
System.out.println(myWebservice.add(2, 3));
System.out.println(myWebservice.minus(2, 3));
} catch (MalformedURLException e) {
e.printStackTrace();
控制台输出正确结果 5和 -1
第三部,spring spring-mvc cxf结合
1.创建项目并添加spring spring-mvc cxf的依赖
&properties&
&spring.version&4.3.3.RELEASE&/spring.version&
&/properties&
&dependencies&
&dependency&
&groupId&junit&/groupId&
&artifactId&junit&/artifactId&
&version&4.12&/version&
&scope&test&/scope&
&/dependency&
&!--spring--&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-core&/artifactId&
&version&${spring.version}&/version&
&/dependency&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-beans&/artifactId&
&version&${spring.version}&/version&
&/dependency&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-context&/artifactId&
&version&${spring.version}&/version&
&/dependency&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-context-support&/artifactId&
&version&${spring.version}&/version&
&/dependency&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-test&/artifactId&
&version&${spring.version}&/version&
&/dependency&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-web&/artifactId&
&version&${spring.version}&/version&
&/dependency&
&dependency&
&groupId&org.springframework&/groupId&
&artifactId&spring-webmvc&/artifactId&
&version&${spring.version}&/version&
&/dependency&
&dependency&
&groupId&org.apache.cxf&/groupId&
&artifactId&cxf-rt-transports-http&/artifactId&
&version&3.1.8&/version&
&/dependency&
&!--web service 以下都是cxf必备的--&
&!--org.apache.cxf.transport.servlet.CXFServlet--&
&dependency&
&groupId&org.apache.cxf&/groupId&
&artifactId&cxf-rt-transports-http&/artifactId&
&version&3.1.8&/version&
&/dependency&
&!--不加这个包会报错Unable to locate spring NamespaceHandler for XML schema namespace [http://cxf.apache.org/jaxws]--&
&dependency&
&groupId&org.apache.cxf&/groupId&
&artifactId&cxf-rt-frontend-jaxws&/artifactId&
&version&3.1.8&/version&
&/dependency&
&!--java实现webservice,不部署到tomcat,需要jetty包支持--&
&dependency&
&groupId&org.apache.cxf&/groupId&
&artifactId&cxf-rt-transports-http-jetty&/artifactId&
&version&3.1.8&/version&
&/dependency&
&/dependencies&
2.webservice服务端接口及实现类
@WebService
public interface MyWebService {
int add(@WebParam(name = "firstA")int a, @WebParam(name = "firstB")int b);
int minus(@WebParam(name = "secondA")int a, @WebParam(name = "secondB")int b);
@WebService(endpointInterface = "com.lida.dream_webservice.server.MyWebService")
public class MyWebserviceImpl implements MyWebService {
public int add(int a, int b) {
System.out.println(a+"+"+b+"="+(a+b));
return a+b;
public int minus(int a, int b) {
System.out.println(a+"-"+b+"="+(a-b));
return a-b;
3.配置spring-web.xml及spring-webservice.xml
&?xml version="1.0" encoding="UTF-8"?&
&beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"&
&!--配置springmvc--&
&!--1.开启springmvc注解模式--&
&!--简化配置:
(1)主动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
(2)提供一系列功能:数据绑定,数字和日期的format @NumberFormt @DataTimeFormat,xml json默认的读写支持--&
&mvc:annotation-driven/&
&!--servlet-mapping--&
&!--2静态资源默认的servlet配置,(1)允许对静态资源的处理:js,gif
(2)允许使用“/”做整体映射--&
&!-- 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL--&
&mvc:default-servlet-handler/&
&!--3:配置jsp 显示viewResolver--&
&bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&
&property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&
&property name="prefix" value="/WEB-INF/views/"/&
&property name="suffix" value=".jsp"/&
&!-- 4自动扫描且只扫描@Controller --&
&context:component-scan base-package="com.lida.dream_webservice" /&
&!-- 定义无需Controller的url&-&view直接映射 --&
&mvc:view-controller path="/" view-name="redirect:/index"/&
&?xml version="1.0" encoding="UTF-8"?&
&beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-lazy-init="true"&
&!-- 引入CXF Bean定义如下,早期的版本中使用 --&
&!--&import resource="classpath:META-INF/cxf/cxf.xml" /&
&import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /&
&import resource="classpath:META-INF/cxf/cxf-servlet.xml" /&--&
&!--发布webservice--&
&!-- WebService的实现Bean定义 --&
&!--web.xml配置了webservice的访问路径/server/*,那么/server/web-publish?wsdl就是该webservice的访问路径--&
&bean id="webserviceServer" class="com.lida.dream_webservice.server.MyWebserviceImpl" /&
&!-- jax-ws endpoint定义
&jaxws:endpoint id="myService"
implementor="#webserviceServer"
address="/web-publish" &
&/jaxws:endpoint&
&!--发布webservice--&
4.配置web.xml
&web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="/xml/ns/javaee"
xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_2_5.xsd"
version="2.5"&
&welcome-file-list&
&welcome-file&index.jsp&/welcome-file&
&/welcome-file-list&
&context-param&
&param-name&contextConfigLocation&/param-name&
&param-value&
classpath:spring/spring-webservice.xml
&/param-value&
&/context-param&
&servlet-name&springServlet&/servlet-name&
&servlet-class&org.springframework.web.servlet.DispatcherServlet&/servlet-class&
&init-param&
&param-name&contextConfigLocation&/param-name&
&param-value&classpath:spring/spring-web.xml&/param-value&
&/init-param&
&load-on-startup&1&/load-on-startup&
&/servlet&
&servlet-mapping&
&servlet-name&springServlet&/servlet-name&
&url-pattern&/&/url-pattern&
&/servlet-mapping&
&filter-name&characterEncodingFilter&/filter-name&
&filter-class&org.springframework.web.filter.CharacterEncodingFilter&/filter-class&
&init-param&
&param-name&encoding&/param-name&
&param-value&UTF-8&/param-value&
&/init-param&
&init-param&
&param-name&forceEncoding&/param-name&
&param-value&true&/param-value&
&/init-param&
&filter-mapping&
&filter-name&characterEncodingFilter&/filter-name&
&url-pattern&/*&/url-pattern&
&/filter-mapping&
&!--Spring MVC是通过DispatcherServlet来加载Spring配置文件的,因此不需要在web.xml中配置ContextLoaderListener。
但是CXF却需要通过ContextLoaderListener来加载Spring。--&
&listener&
&listener-class&org.springframework.web.context.ContextLoaderListener&/listener-class&
&/listener&
&!-- 配置CXF框架的核心Servlet
&servlet-name&CXFServlet&/servlet-name&
&servlet-class&org.apache.cxf.transport.servlet.CXFServlet&/servlet-class&
&load-on-startup&2&/load-on-startup&
&/servlet&
&servlet-mapping&
&servlet-name&CXFServlet&/servlet-name&
&url-pattern&/server/*&/url-pattern&
&/servlet-mapping&
&/web-app&
这里一定要注意spring-mvc与cxf使用不同的容器加载,否则会报错:
springmvc是不需要ContextLoaderListener来加载管理bean的,DispatcherServlet它有自己的容器,主要用于加载除控制器层的bean,DispatcherServlet属于子容器。
cxf需要ContextLoaderListener,cxf需要ContextLoaderListener是spring管理bean的父容器,一般用于加载非控制器层的bean。
子容器可以访问父容器中的bean,而父容器不能访问子容器。
可以把基本web请求的controller等这些bean放到spring-web.xml中,让DispatcherServlet去加载管理spring-web.xml。
把webservice相关配置到另外一个xml文件中,比如spring-webservice.xml,
让ContextLoaderListener去加载管理spring-webservice.xml和其他spring文件()spring-mysql.xml,spring-jpa.xml等),
这样,就互不影响了。
而且:ContextLoaderListene默认会加载applicationContext.xml这个名字的文件,如果定义为spring-context.xml会报错,重命名需要context-param
此时配置tomcat启动项目后便暴露了wsdl,在浏览器访问http://localhost:8080/server/web-publish?wsdl
public class ClientForCXF {
public static MyWebService getInterFace(){
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setServiceClass(MyWebService.class);
factoryBean.setAddress("http://localhost:8080/server/web-publish");
return (MyWebService) factoryBean.create();
public static void main(String[] args) {
MyWebService myWebService = getInterFace();
System.out.println("client: "+myWebService.add(1,3));
以上两种实现webservice的方法仅适用于自己有java的webservice的服务端,自己调用。而往往服务端可能由别的语言实现,或者服务端并非我们自己实现,我们没有服务端接口,我们只能获得暴漏的wsdl,并进行调用,这就需要使用wsdl2java生成该wsdl的java客户端并调用了
第四步:wsdl2java生成客户端代码并调用
打开cmd(因为我们配置了环境变量,所以可以直接使用wedl2java命令)
wsdl2java -encoding utf-8 -d F:\IdeaProjects\dream-webservice\wsdl2javatest http://localhost:8080/server/web-publish?wsdl
遍在wsdl2javatest生成了该wsdl的客户端代码
这时,我们变可以写客户端并调用了
public class Client {
public static void main(String args[]) {
MyWebserviceImplService service = new MyWebserviceImplService();
MyWebService myWebService = service.getMyWebserviceImplPort();
System.out.println(myWebService.add(1,2));
附上git源码,大家可以fork下来学习一下,大神勿喷,请多指教。
相关标签:
本文原创发布于慕课网 ,转载请注明出处,谢谢合作!
请登录后,发表评论
评论(Enter+Ctrl)
评论加载中...
评论加载中...
Copyright (C)
All Rights Reserved | 京ICP备 号-2

我要回帖

更多关于 spring mvc 框架搭建 的文章

 

随机推荐