一朋友最近他们公司要求必须马上做个基于cxf的webservice出来,并要求讲解。。。他刚接触webservice,各种找视频找代码不给力,关键是时间紧。。。。。。
纯属被他逼出来做这个简易搭建说明
------割-------
说下框架,就搭建来说cxf 比起axis、xfire有优势。采用spring来管理,这还用说。。。。。。
先提供我做的简单实例(包括服务器端和客户端)下载,不需要了解搭建知识的就直接拖去用,不用浪费时间了,里面有大量的注释来解释说明。click me
蛋疼的,请继续。。。。。。
额,采用maven来搭建,maven知识不足的同学,请去补下maven的搭建知识先。。。。。。
蛋更疼的,请继续。。。。。。
首先新建一个maven工程。
提供的实例里不是叫webservice,是叫ws,一个意思了。。。
介个时候,工程就建好了,如下图,ws2工程
选中工程名字,然后alt+回车键调出工程属性界面:
首先修改jdk,为1.6以上
再去project facets界面去修改
点cxf时是不是需要把dynamic web module改为2.5呀,是不是改不了2.5呀,表担心,找到工程目录下的.settings/org.eclipse.wst.common.project.facet.core.xml 这个文件里把2.3改为2.5,顺便把jdk也改为1.6,刷新下工程。
其实开了个玩笑,没有专门的cxf,所以这里不需要勾选cxf,哈哈。。。。
自己动手吧。。。。。。
先打开pom.xml加上spring和cxf的相关包配置
好了。。。。。
web.xml 吧:
直接copy过去粘贴好了:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/webservices/*</url-pattern>
</servlet-mapping>
</web-app>
此处需要注意的是,CXFService下面的/webservices/*,这个后面会提到。
再在src/main/resources下面新建个 applicationContext.xml 吧
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- context -->
<import resource="spring.xml" />
<import resource="spring-webservice.xml" />
</beans>
spring.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:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 自动扫描dao和service包(自动注入) -->
<context:component-scan base-package="com.test" />
</beans><strong>
</strong>
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://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<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" />
<!--
implementor的地址就是webservice接口实现类里的@Component里的value值
address的值是随意的,决定生成wsdl的名字就是testWService.wsdl
-->
<jaxws:endpoint implementor="#testWServcieImpl"
address="/testWService" />
</beans>
好吧,这里有一些解释,需要结合后面的java接口和实现类来说明。。。。。
先看下结构:
1是普通的service接口,跟我们原来工程里大部分service接口一致。
2是专门的webservice接口,他们本身其实就是普通接口的一种扩展,加了一些webservice的annotation而已。
普通service接口略去。
看下webservice接口
package com.test.ws;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* webservice 测试接口
*
* @Author Ellrien
* @Date 2014年6月13日 上午9:58:28
* @Version 1.0
*/
@WebService
public interface TestWServiceI {
/**
* 加法运算
* @param param1
* @param param2
* @return String
*/
public String getSum(@WebParam(name = "param1") int x,
@WebParam(name = "param2") int y);
}
可以看出其实就是加了@webservice 以及每个方法里的参数都标上了@webparam
再看接口的实现类
package com.test.ws.impl;
import javax.jws.WebMethod;
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.test.service.TestServiceI;
import com.test.ws.TestWServiceI;
/**
* webservice 实现类
*
* @Author Ellrien
* @Date 2014年6月13日 上午10:01:58
* @Version 1.0
*/
/*
* targetNamespace 的值可以随便写,将会在接口列表中看到。
* serviceName 的值也可以随便写,将会在接口列表中看到。
* portName的值也可以乱写,将会在生成客户端时用到。
*
* 以上三个参数可写可不写。
*
* @Component 的value值,需要跟cxf配置文件里的implementor保持一致
*/
@WebService(targetNamespace = "http://com.test.lalala/", serviceName = "testService", portName = "testServicePort")
@Component(value = "testWServcieImpl")
public class TestWServcieImpl implements TestWServiceI {
@Autowired
TestServiceI service;
@WebMethod
public String getSum(int x, int y) {
int sum = service.getSum(x, y);
return String.valueOf(sum);
}
}
其实还是一些annotation,而且注释已经写得很明了,包括每个参数该怎么写。如果有疑问请在下面留言。。。。
至此,这个工程就可以发布了。。。。。看下发布后的结果:
输入http://localhost/ws/webservices
看到下图:
这表示webservice发布成功!
具体解释下1234
----------割-----------------------
下面介绍客户端的。
我客户端也写在这个工程里面了。就当做另一种单元测试好了。
按理应该在另一个工程里,按照service+impl的方式来做比较合适。
package com.test.client;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.test.ws.TestWServiceI;
/**
* 这个环节是独立的,完全是客户端,你可以按照下面的serviceI 和 serviceImpl 对应关系来写
*
* 此客户端是基于cxf来做的。需要jdk1.6以上
*
* @Author Ellrien
* @Date 2014年6月13日 下午2:20:13
* @Version 1.0
*/
public class ClientServiceBean {
private static URL wsdl_url = null;
private static Service service = null;
private static void init() throws MalformedURLException {
// WSDL地址
wsdl_url = new URL("http://localhost/ws/webservices/testWService?wsdl");
// new QName(实现类里的targetNamespace,实现类里的serviceName)
QName serviceName = new QName("http://com.test.lalala/", "testService");
service = Service.create(wsdl_url, serviceName);
}
private static void test() {
int i = 1;
int j = 2;
TestWServiceI clientService = service
.getPort(TestWServiceI.class);// 通关service来还原成相同的service接口
String result = clientService.getSum(i, j);
System.out.println("调用webservice的结果为:" + result);
}
public static void main(String[] args) {
try {
init();
test();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
其实已经注释得很清楚,如果你上面的服务器端都看懂了,这个客户端就是傻瓜式的。
----------------割----------------------
福利如下:介绍一种最简单的客户端生成方式,采用的是axis自动生成webservice客户端,无论你用axis、cxf、xfire哪种生成的wsdl都能自动生成java代码,谁用谁知道。。。。妈妈再也不用担心我写不出webservice客户端了。。。。