maven+SpringMvc发布WebService(附录排除问题小过程)

源码下载


简介:项目使用maven管理,SpringMvc框架。但是项目结构只在配置文件中有所体现(引入spring相关jar),未进行实际分模块开发。

过程:

1.创建maven project

2.编辑配置文件总共4个,分别是

   (1)spring-mvc.xml

   (2)spring-webservice.xml

   (3)web.xml

   (4)pom.xml

其中前两个spring开头的配置文件可以随便命名,文件寻址配置在web.xml中

文件内容分别列出:

(1)spring-mvc.xml



	
	
	
		
		
		
	 
	


(2)spring-webservice.xml


	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">
	
	
			address="/webservice111">
	


 (3)web.xml

	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	
		index.jsp
	
	
	
		contextConfigLocation
		 classpath:spring/spring-webservice.xml 
	
	
		springServlet
		org.springframework.web.servlet.DispatcherServlet
		
		
			contextConfigLocation
			classpath:spring/spring-mvc.xml
		
		1
	
	
		springServlet
		/
	
	
		characterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
		
			forceEncoding
			true
		
	
	
		characterEncodingFilter
		/*
	 
	
		org.springframework.web.context.ContextLoaderListener
	 
	
		CXFServlet
		org.apache.cxf.transport.servlet.CXFServlet
		2
	
	
	
		CXFServlet
		/msc111/*
	

 注解1和注解2分别配置了spring-mvc.xml和spring-webservice.xml

两个文件不可以使用同一个容器加载。

相关配置的原因百度springmvc父子容器进行不充理解。

(4)pom.xml


	4.0.0
	com.msc
	msc
	0.0.1-SNAPSHOT
	war
	
		4.3.3.RELEASE
	
	
		
			junit
			junit
			4.12
			test
		 
		
			org.springframework
			spring-core
			${spring.version}
		
		
			org.springframework
			spring-beans
			${spring.version}
		
		
			org.springframework
			spring-context
			${spring.version}
		
		
			org.springframework
			spring-context-support
			${spring.version}
		
		
			org.springframework
			spring-test
			${spring.version}
		
		
			org.springframework
			spring-web
			${spring.version}
		
		
			org.springframework
			spring-webmvc
			${spring.version}
		
		 
		
			org.apache.cxf
			cxf-rt-transports-http
			3.1.8
		 
		
			org.apache.cxf
			cxf-rt-frontend-jaxws
			3.1.8
		
		
	
	
	
		
			
				org.apache.tomcat.maven
				tomcat7-maven-plugin
				
					8080
					/
				
			
			
			
				org.apache.maven.plugins
				maven-surefire-plugin
				
					true
				
			
		
	

WebService发布方法:

与一般的maven项目运行相同。项目右键---run as---maven-build

运行如果成功,则可以在浏览器输入:

http://localhost:8080/msc111/webservice111?wsdl

该地址基于spring-webservice.xml中的注解1和web.xml中的注解3

浏览器返回内容为

maven+SpringMvc发布WebService(附录排除问题小过程)_第1张图片

最后使用紫色方框中的命名空间和名称来进行测试。


在项目中创建一个类,内容如下

package com.msc.client;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.msc.service.MyWebService;

public class WebServiceClient {
	public static void main(String args[]) {
		try {
			URL url = new URL("http://localhost:8080/msc111/webservice111?wsdl"); 
			// 命名空间 及 名称
			QName qName = new QName("http://impl.service.msc.com/",
					"MyWebserviceImplService");
			Service service = Service.create(url, qName);
			MyWebService myWebservice = service.getPort(MyWebService.class);
			System.out.println(myWebservice.plus(2, 3));
			System.out.println(myWebservice.minus(2, 3));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
}

在该类上右键 ---run as --- java application 就可以访问webservice的远程服务计算加法减法。

maven build失败,如何查找问题所在?

打开命令行,使用cd命令转到当前项目中

然后执行命令:

mvn dependency:analyze

maven+SpringMvc发布WebService(附录排除问题小过程)_第2张图片

开始按照别人的方法做,就一直不能执行maven build,最终分析结果是,该文作者重复引用了jar文件所致。



生成客户端命令:

首先下载安装cxf,然后配置环境变量

打开命令行

切换到项目目录下输入:

wsdl2java -encoding utf-8 -d E:\workspace\mschttp://localhost:8080/msc111/webservice111?wsdl

回车

项目目录会多出若干package文件夹。可以直接放在客户端项目里使用







你可能感兴趣的:(孤陋寡闻)