使用 Eclipse 创建 Maven SOAP Web 服务的步骤。
前提条件:
- Java >= 1.5
- Maven 安装
- Eclipse Maven Plugin
创建步骤:
创建一个新的 Maven 项目,包括 packaging type as war
打开 pom 文件,增加 spring 依赖
pom.xml
001 | < 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" > |
002 | < modelVersion >4.0.0</ modelVersion > |
003 | < groupId >com.test</ groupId > |
004 | < artifactId >cxf-spring-test</ artifactId > |
005 | < version >0.1</ version > |
006 | < packaging >war</ packaging > |
007 | < properties > |
008 | < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > |
009 | < spring.version >3.0.5.RELEASE</ spring.version > |
010 | < slf4j.version >1.6.4</ slf4j.version > |
011 | < cxf.version >2.4.1</ cxf.version > |
012 | </ properties > |
013 |
014 | < repositories > |
015 | < repository > |
016 | < id >maven2-repository.java.net</ id > |
017 | < name >Java.net Repository for Maven</ name > |
018 | < url >http://download.java.net/maven/2/</ url > |
019 | < layout >default</ layout > |
020 | </ repository > |
021 | </ repositories > |
022 |
023 | < build > |
024 | < finalName >${project.artifactId}</ finalName > |
025 | < plugins > |
026 | < plugin > |
027 | < artifactId >maven-compiler-plugin</ artifactId > |
028 | < version >2.3.2</ version > |
029 | < configuration > |
030 | < source >1.6</ source > |
031 | < target >1.6</ target > |
032 | </ configuration > |
033 | </ plugin > |
034 | < plugin > |
035 | < groupId >org.mortbay.jetty</ groupId > |
036 | < artifactId >jetty-maven-plugin</ artifactId > |
037 | < version >7.4.2.v20110526</ version > |
038 | < configuration > |
039 | < scanIntervalSeconds >10</ scanIntervalSeconds > |
040 | < connectors > |
041 | < connector implementation = "org.eclipse.jetty.server.nio.SelectChannelConnector" > |
042 | < port >7009</ port > |
043 | </ connector > |
044 | </ connectors > |
045 | < webAppConfig > |
046 | < contextPath >/${project.artifactId}</ contextPath > |
047 | </ webAppConfig > |
048 | </ configuration > |
049 | </ plugin > |
050 | </ plugins > |
051 | </ build > |
052 | < dependencies > |
053 | <!-- CXF Dependencies --> |
054 | < dependency > |
055 | < groupId >jta</ groupId > |
056 | < artifactId >jta</ artifactId > |
057 | < version >1.0.1b</ version > |
058 | </ dependency > |
059 | < dependency > |
060 | < groupId >org.apache.cxf</ groupId > |
061 | < artifactId >cxf-rt-core</ artifactId > |
062 | < version >${cxf.version}</ version > |
063 | </ dependency > |
064 | < dependency > |
065 | < groupId >org.apache.cxf</ groupId > |
066 | < artifactId >cxf-rt-frontend-simple</ artifactId > |
067 | < version >${cxf.version}</ version > |
068 | </ dependency > |
069 | < dependency > |
070 | < groupId >org.apache.cxf</ groupId > |
071 | < artifactId >cxf-rt-frontend-jaxws</ artifactId > |
072 | < version >${cxf.version}</ version > |
073 | </ dependency > |
074 | < dependency > |
075 | < groupId >org.apache.cxf</ groupId > |
076 | < artifactId >cxf-rt-databinding-aegis</ artifactId > |
077 | < version >${cxf.version}</ version > |
078 | </ dependency > |
079 | < dependency > |
080 | < groupId >org.apache.cxf</ groupId > |
081 | < artifactId >cxf-rt-transports-local</ artifactId > |
082 | < version >${cxf.version}</ version > |
083 | </ dependency > |
084 | < dependency > |
085 | < groupId >org.apache.cxf</ groupId > |
086 | < artifactId >cxf-rt-transports-http</ artifactId > |
087 | < version >${cxf.version}</ version > |
088 | </ dependency > |
089 | < dependency > |
090 | < groupId >org.apache.cxf</ groupId > |
091 | < artifactId >cxf-rt-transports-http-jetty</ artifactId > |
092 | < version >${cxf.version}</ version > |
093 | </ dependency > |
094 | < dependency > |
095 | < groupId >org.apache.cxf</ groupId > |
096 | < artifactId >cxf-rt-transports-jms</ artifactId > |
097 | < version >${cxf.version}</ version > |
098 | </ dependency > |
099 | < dependency > |
100 | < groupId >org.apache.cxf</ groupId > |
101 | < artifactId >cxf-rt-management</ artifactId > |
102 | < version >${cxf.version}</ version > |
103 | </ dependency > |
104 | < dependency > |
105 | < groupId >org.apache.cxf</ groupId > |
106 | < artifactId >cxf-common-utilities</ artifactId > |
107 | < version >${cxf.version}</ version > |
108 | </ dependency > |
109 | <!-- End of CXF Dependencies --> |
110 |
111 | < dependency > |
112 | < groupId >org.eclipse.jetty</ groupId > |
113 | < artifactId >jetty-servlets</ artifactId > |
114 | < version >7.4.0.v20110414</ version > |
115 | < scope >provided</ scope > |
116 | < exclusions > |
117 | < exclusion > |
118 | < groupId >org.eclipse.jetty</ groupId > |
119 | < artifactId >jetty-client</ artifactId > |
120 | </ exclusion > |
121 | </ exclusions > |
122 | </ dependency > |
123 |
124 | <!-- Spring Dependencies ${spring.version} --> |
125 | < dependency > |
126 | < groupId >org.springframework</ groupId > |
127 | < artifactId >spring-beans</ artifactId > |
128 | < version >${spring.version}</ version > |
129 | </ dependency > |
130 | < dependency > |
131 | < groupId >org.springframework</ groupId > |
132 | < artifactId >spring-jdbc</ artifactId > |
133 | < version >${spring.version}</ version > |
134 | </ dependency > |
135 | < dependency > |
136 | < groupId >org.springframework</ groupId > |
137 | < artifactId >spring-web</ artifactId > |
138 | < version >${spring.version}</ version > |
139 | </ dependency > |
140 | < dependency > |
141 | < groupId >org.springframework</ groupId > |
142 | < artifactId >spring-webmvc</ artifactId > |
143 | < version >${spring.version}</ version > |
144 | </ dependency > |
145 | < dependency > |
146 | < groupId >org.springframework</ groupId > |
147 | < artifactId >spring-orm</ artifactId > |
148 | < version >${spring.version}</ version > |
149 | </ dependency > |
150 | < dependency > |
151 | < groupId >org.springframework</ groupId > |
152 | < artifactId >spring-expression</ artifactId > |
153 | < version >${spring.version}</ version > |
154 | </ dependency > |
155 |
156 | < dependency > |
157 | < groupId >org.apache.geronimo.specs</ groupId > |
158 | < artifactId >geronimo-annotation_1.0_spec</ artifactId > |
159 | < version >1.1.1</ version > |
160 | </ dependency > |
161 |
162 | < dependency > |
163 | < groupId >org.slf4j</ groupId > |
164 | < artifactId >slf4j-log4j12</ artifactId > |
165 | < version >${slf4j.version}</ version > |
166 | < type >jar</ type > |
167 | < scope >compile</ scope > |
168 | </ dependency > |
169 |
170 | </ dependencies > |
171 | </ project > |
在 src/main/webapp/WEB-INF 文件夹中创建 web.xml 文件
Web.Xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
03 | xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" |
04 | xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" |
05 | id = "WebApp_ID" version = "2.5" > |
06 |
07 | <!-- Servlet Mapping for CXFServlet --> |
08 | < servlet > |
09 | < servlet-name >CXFServlet</ servlet-name > |
10 | < servlet-class >org.apache.cxf.transport.servlet.CXFServlet</ servlet-class > |
11 | </ servlet > |
12 | < servlet-mapping > |
13 | < servlet-name >CXFServlet</ servlet-name > |
14 | < url-pattern >/*</ url-pattern > |
15 | </ servlet-mapping > |
16 |
17 | <!-- Context parameters --> |
18 | < context-param > |
19 | < param-name >log4jConfigLocation</ param-name > |
20 | < param-value >classpath:sitemonitor_log4j.properties</ param-value > |
21 | </ context-param > |
22 | < context-param > |
23 | < param-name >log4jExposeWebAppRoot</ param-name > |
24 | < param-value >false</ param-value > |
25 | </ context-param > |
26 | < listener > |
27 | < listener-class >org.springframework.web.util.Log4jConfigListener</ listener-class > |
28 | </ listener > |
29 |
30 | < context-param > |
31 | < param-name >contextConfigLocation</ param-name > |
32 | < param-value >classpath:sitemonitor_appContext.xml</ param-value > |
33 | </ context-param > |
34 |
35 | < listener > |
36 | < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > |
37 | </ listener > |
38 | </ web-app > |
现在创建一个新的包,并创建服务接口:
01 | package com.test; |
02 |
03 | import javax.jws.WebService;<a href= "http://my.oschina.net/u/142217" target= "_blank" rel= "nofollow" > @WebService </a> |
04 | public interface CalculatorIntf |
05 | { |
06 | public long add( long num1, long num2); |
07 |
08 | public long subtract( long num1, long num2); |
09 |
10 | public long multiply( long num1, long num2); |
11 | } |
创建一个接口实现类
01 | package com.test; |
02 |
03 | public class CalculatorImpl implements CalculatorIntf |
04 | { |
05 | public long add( long num1, long num2) |
06 | { |
07 | return (num1 + num2); |
08 | } |
09 |
10 | public long subtract( long num1, long num2) |
11 | { |
12 | return (num1 - num2); |
13 | } |
14 |
15 | public long multiply( long num1, long num2) |
16 | { |
17 | return (num1 * num2); |
18 | } |
19 |
20 | } |
创建 spring 的应用 context 文件,并添加到应用类路径中
Application Context
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < beans xmlns = "http://www.springframework.org/schema/beans" |
03 | xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop = "http://www.springframework.org/schema/aop" |
04 | xmlns:context = "http://www.springframework.org/schema/context" |
05 | xmlns:jee = "http://www.springframework.org/schema/jee" xmlns:lang = "http://www.springframework.org/schema/lang" |
06 | xmlns:p = "http://www.springframework.org/schema/p" xmlns:tx = "http://www.springframework.org/schema/tx" |
07 | xmlns:util = "http://www.springframework.org/schema/util" xmlns:jaxws = "http://cxf.apache.org/jaxws" |
08 | xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
09 | http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd |
10 | http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd |
11 | http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd |
12 | http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd |
14 | http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> |
15 |
16 | <!-- Initiliaing Client Webservices --> |
17 | < import resource = "classpath:META-INF/cxf/cxf.xml" /> |
18 | < import resource = "classpath:META-INF/cxf/cxf-servlet.xml" /> |
19 |
20 | < bean id = "calculatorService" class = "com.test.CalculatorImpl" /> |
21 |
22 | <!-- JAX-WS Service Endpoint --> |
23 | < jaxws:endpoint id = "calcService" implementor = "#calculatorService" address = "/calculatorService" /> |
24 | </ beans > |
现在打开命令行窗口,并进入项目目录。
运行 maven 命令 : mvn clean package jetty:run -DskipTests
接下来打开浏览器访问 http://localhost:7009/cxf-spring-test
你将看到 wsdl 链接,点击可以查看 wsdl 文件。
下一步你可以创建一个 SOAP 客户端来测试这个 Web 服务。