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

<?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">
	<!-- 开启注解驱动 -->
	<mvc:annotation-driven />
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean> <!-- 配置Controller扫描器 -->
	<context:component-scan base-package="com.msc.controller" />
</beans>

(2)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: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">
	<bean id="webserviceServer" class="com.msc.service.impl.MyWebserviceImpl" />
	<!-- 注解2 -->
	<jaxws:endpoint id="myService" implementor="#webserviceServer"
		address="/webservice111">
	</jaxws:endpoint>
</beans>

 (3)web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<!-- 注解1 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value> classpath:spring/spring-webservice.xml </param-value>
	</context-param>
	<servlet>
		<servlet-name>springServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 注解2 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/spring-mvc.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>
		<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>
	<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>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<!-- 注解3 -->
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/msc111/*</url-pattern>
	</servlet-mapping>
</web-app>


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

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

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

(4)pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.msc</groupId>
	<artifactId>msc</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<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>
		<!--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>
	<!-- 添加tomcat插件 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<port>8080</port>
					<path>/</path>
				</configuration>
			</plugin>
			<!-- 编译时测试内容报错,需要加 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<testFailureIgnore>true</testFailureIgnore>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

WebService发布方法:

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

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

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

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

浏览器返回内容为


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


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

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 build,最终分析结果是,该文作者重复引用了jar文件所致。



生成客户端命令:

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

打开命令行

切换到项目目录下输入:

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

回车

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







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值