轉自:http://blog.csdn.net/u011165335/article/details/51345224
一、概覽
方式1:
HttpClient:可以用來調用webservie服務,也可以抓取網頁數據
版本1:HttpClient3.0.x
版本2:HttpClient4.x.x(目前最新4.5.2)
這2個版本的使用方式不一樣;變動較大
方式2:純Java(自帶API) jws
方式3:cxf框架
方式4:axis2框架
准備工作:
1.了解wsimport java自帶的一個命令(建議使用jdk7,穩定支持)
作用:將wsdl文件生成本地代理(java代碼),方便調用
語法 wsimport [opations] <wsdl_uri>
- wsdl_uri:wsdl 的統一資源標識符
- d :指定要輸出的文件的位置
- s :表示要解析java的源碼 ,默認解析出的是class字節碼
- p : 指定輸出的包名
1. 獲取服務路徑:比如
http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL
2. 獲取wsdl文件.建議使用JDK1.6以上的版本的wsimport命令
進入dos的桌面:
方式1:wsimport http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL
這種默認只會生成class,且會生成默認的包
方式2:生成源碼,指定包和路徑
wsimport -s ./ -p cn.aa http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL
3.可以把class文件打成jar包 jar cvf test.jar 打包目錄
4.放在你的項目中進行調用:
- public static void main(String[] args) {
- MobileCodeWS mobileCodeWs=new MobileCodeWS();
- MobileCodeWSSoap mobileCodeWSSoap=mobileCodeWs.getMobileCodeWSSoap();
- String tel=mobileCodeWSSoap.getMobileCodeInfo("183735xxxx",null);
- System.out.println(tel);
- }
2.規范了解
JAVA 中共有三種WebService 規范,分別是JAX-WS(JAX-RPC)、JAXM&SAAJ、JAX-RS。
1. Jaxws(掌握)
JAX-WS 的全稱為 Java API for XML-Based Webservices ,早期的基於SOAP 的JAVA 的Web 服務規范JAX-RPC(java API For XML-RemoteProcedure Call)目前已經被JAX-WS 規范取代。從java5開始支持JAX-WS2.0版本,Jdk1.6.0_13以后的版本支持2.1版本,jdk1.7支持2.2版本。
Jaxws開發的webservice傳輸soap協議。
2JAXM&SAAJ(了解)
JAXM(JAVA API For XML Message)主要定義了包含了發送和接收消息所需的API,SAAJ(SOAP With Attachment APIFor Java,JSR 67)是與JAXM 搭配使用的API,為構建SOAP 包和解析SOAP 包提供了重要的支持,支持附件傳輸等,JAXM&SAAJ 與JAX-WS 都是基於SOAP 的Web 服務,相比之下JAXM&SAAJ 暴漏了SOAP更多的底層細節,編碼比較麻煩,而JAX-WS 更加抽象,隱藏了更多的細節,更加面向對象,實現起來你基本上不需要關心SOAP 的任何細節
3. JAX-RS(掌握)
JAX-RS 是JAVA 針對REST(Representation State Transfer)風格制定的一套Web 服務規范,由於推出的較晚,該規范(JSR 311,目前JAX-RS 的版本為1.0)並未隨JDK1.6 一起發行。
Rest定義可以自行搜索
jax-RS可以發布 rest風格webservice,因為rest的webservice不采用soap傳輸,直接采用http傳輸,可以返回xml或json,比較輕量。
以后可能會流行Rest風格的
二、簡單例子
1.httpClient3.x.x
此方式不需要wsimport命令
- // 通過Http-Client 框架來模擬實現 Http請求--get
- public static String getMobileCodeInfo1(String mobileCode, String userID)
- throws HttpException, IOException {
- HttpClient client = new HttpClient();
- GetMethod getMethod=new GetMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="
- + mobileCode + "&userID=" + userID);
- //執行,得到消息碼
- int code = client.executeMethod(getMethod);
- System.out.println("消息碼:"+code);
- String result="";
- if (code==HttpURLConnection.HTTP_OK) {
- //得到執行結果
- result = getMethod.getResponseBodyAsString();
- System.out.println(result);
- }
- return result;
- }
- // 通過Http-Client 框架來模擬實現 Http請求--post
- // 需要導入3個jar包,本demo的jar包版本是3.1.0
- // 目前最新的是4.5.2,使用方式也發生了變化
- public static String getMobileCodeInfo2(String mobileCode, String userID)
- throws HttpException, IOException {
- // 輸入服務網址
- HttpClient client = new HttpClient();
- // GetMethod
- PostMethod post = new PostMethod(
- "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
- // 設置參數
- post.setParameter("mobileCode", mobileCode);
- post.setParameter("userID", userID);
- // client.setTimeout(newTimeoutInMilliseconds);
- // 執行,返回一個結果碼
- int code = client.executeMethod(post);
- System.out.println("結果碼:" + code);
- // 獲取xml結果
- String result = post.getResponseBodyAsString();
- System.out.println("結果:" + result);
- //釋放連接
- post.releaseConnection();
- return result;
- }
- // Post請求 :通過Http-Client 框架來模擬實現 Http請求
- //從xml中獲取請求參數
- //SOAP1.1方式
- //問題:soap怎么定義,這里已經返回接結果,但是沒有手機號信息,也就返回的結果匹配,不知道為什么
- //估計是配置文件有問題
- //soap1.1
- @Test
- public void soap() throws Exception{
- HttpClient client=new HttpClient();
- PostMethod postMethod=new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
- //3.設置請求參數
- postMethod.setRequestBody(new FileInputStream("D:/soap.xml")); //文件名自定義
- //修改請求的頭部
- postMethod.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
- //4.執行請求 ,結果碼
- int code=client.executeMethod(postMethod);
- System.out.println("結果碼:"+code);
- //5. 獲取結果
- String result=postMethod.getResponseBodyAsString();
- System.out.println("Post請求的結果:"+result);
- }
- public static void main(String[] args) throws IOException {
- // getMobileInfo("13476699xxx", "");
- getMobileCodeInfo1("13476699xxx", "");//
- // getMobileCodeInfo2("13476699xxx", "");//
- //soap,利用xml傳輸參數
- }
其中:請求參數封裝在soap.xml中
請求規范服務方會提供給你的
soap.xml請求內容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
- <soap:Body>
- <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
- <mobileCode>1347669xxxx</mobileCode>
- <userID></userID>
- </getMobileCodeInfo>
- </soap:Body>
- </soap:Envelope>
httpClient4.x.x不做演示,感興趣的可以去查查,資料很多
2.java自帶API實現
2.1 類發布
2.1.1 使用默認設置
- @WebService
- //默認服務名會加上Service
- public class PhoneInfoWS {
- //默認方法名getPhoneInfo
- //默認參數名arg0
- //默認返回值名字:return
- //targetNamespace默認情況下為倒置的包名
- public Phone getPhoneInfo(String OSName) {
- Phone p=new Phone();
- if ("Android".equalsIgnoreCase(OSName)) {
- p.setOS(OSName);
- p.setOwn("Google");
- p.setScale(80);
- }else if("IOS".equalsIgnoreCase(OSName)){
- p.setOS(OSName);
- p.setOwn("Apple");
- p.setScale(14);
- }else if("windows Phone".equalsIgnoreCase(OSName)){
- p.setOS(OSName);
- p.setOwn("Microsoft");
- p.setScale(4);
- }else{
- p.setOS("others");
- p.setOwn("others");
- p.setScale(2);
- }
- return p;
- }
- //靜態,非public方法不會被自動發布
- public static String say(String city){
- return "hello"+city;
- }
- //訪問時自動生成一個描述文件xml+xsd約束文件
- public static void main(String[] args) {
- //implementor要發布的對象,一般是接口的實現類
- String address1="http://127.0.0.1/PhoneInfoWS1?WSDL";
- // String address2="http://127.0.0.1/PhoneInfoWS2?WSDL";
- Endpoint point = Endpoint.publish(address1, new PhoneInfoWS());
- Endpoint.publish(address1, new PhoneInfoWS());
- // Endpoint.publish(address2, new PhoneInfoWS());
- //關閉發布
- // point.stop();
- }
- }
2.1.2自定義設置
- @WebService(serviceName="PhoneInfoService",targetNamespace="http://PhoneService.web.com/")
- //默認服務名會加上Service
- public class PhoneInfoWS1 {
- @WebMethod(operationName="getPhoneInfo")
- public @WebResult(name="phone") Phone getPhoneInfo(@WebParam(name="OSName") String OSName) {
- Phone p=new Phone();
- if ("Android".equalsIgnoreCase(OSName)) {
- p.setOS(OSName);
- p.setOwn("Google");
- p.setScale(80);
- }else if("IOS".equalsIgnoreCase(OSName)){
- p.setOS(OSName);
- p.setOwn("Apple");
- p.setScale(14);
- }else if("windows Phone".equalsIgnoreCase(OSName)){
- p.setOS(OSName);
- p.setOwn("Microsoft");
- p.setScale(4);
- }else{
- p.setOS("others");
- p.setOwn("others");
- p.setScale(2);
- }
- return p;
- }
- //靜態,非public方法不會被自動發布
- public static String say(String city){
- return "hello"+city;
- }
- //屏蔽要發布的方法
- @WebMethod(exclude=true)
- public String say1(String city){
- return "hello"+city;
- }
- public String say2(String city){
- return "hello"+city;
- }
- //訪問時自動生成一個描述文件xml+xsd約束文件
- public static void main(String[] args) {
- //implementor要發布的對象,一般是接口的實現類
- String address1="http://127.0.0.1/PhoneInfoWS1?WSDL";
- // String address2="http://127.0.0.1/PhoneInfoWS2?WSDL";
- Endpoint point = Endpoint.publish(address1, new PhoneInfoWS1());
- point.toString();
- Endpoint.publish(address1, new PhoneInfoWS1());
- // Endpoint.publish(address2, new PhoneInfoWS1());
- //關閉發布
- // point.stop();
- }
- }
注意:
1. 在類上添加@WebService注解,代表發布一個WebService服務
2. 通過EndPoint(端點服務)發布一個webService。Endpoint也是jdk提供的一個專門用於發布服務的類,它的publish方法接收兩個參數,
一個是本地的服務地址,二是提供服務的類。它位於javax.xml.ws.*包中。
3. Endpoint.publish(String address, Object implementor) 靜態方法在給定地址處針對指定的實現者對象創建並發布端點
4. 默認public修飾的方法自動發布,靜態方法不會公布
5. 如果希望某個方法不對外公開,可以在方法上添加@WebMethod(exclude=true),阻止對外公開。
6. 如果一個類上,被添加了@WebService注解,則必須此類至少有一個可以公開的方法,否則將會啟動失敗。
7.異常處理:
報錯:
com.sun.xml.internal.ws.model.RuntimeModelerException: runtime modeler error: Wrapper class com.web.PhoneService.jaxws.GetPhoneInfo is not found.
Have you run APT to generate them?
解決辦法:jdk7就可以了
8.測試方法:
a.利用上面說的wsimport生成本地代理,然后調用里面的代碼進行測試就可以了,這里就省略了
b.更方便的測試是使用Eclipse的Web Services Explorer 或者Myeclipse的SOAP Web Services Explorer進行測試
把鏈接:比如 http://127.0.0.1/PhoneInfoWS1?WSDL 放入到瀏覽器的WSDL地址欄就可以了
2.2接口發布
- @WebService
- public interface JobService {
- public String getJobInfo();
- }
- /*
- * 默認只會發布接口的方法,實現類自定義的方法不會被發布了
- * 接口和實現類都需要加注解修飾
- *
- * 聯想Spring的注入只需在實現類加入注解即可
- */
- @WebService(serviceName="JobServiceImplService",
- endpointInterface="com.web.InterfaceService.JobService")
- public class JobServiceImpl implements JobService{
- @Override
- public String getJobInfo() {
- // TODO Auto-generated method stub
- return "java開發|前端開發|數據庫開發";
- }
- // 實現類自定義的方法不會被發布
- public String say(String name){
- return "hello "+name;
- }
- public static void main(String[] args) {
- String address="http://127.0.0.1/JobServiceImpl?WSDL";
- Endpoint.publish(address, new JobServiceImpl());
- System.out.println(address);
- }
- }
3.CXF框架
Apache CXF 是一個開源的 Services 框架,是XFire和Celtix項目的結合產品,CXF 幫助您來構建和開發 Services 這些 Services 可以支持多種協議,比如:SOAP、POST/HTTP、RESTful HTTP CXF 大大簡化了 Service可以天然地和 spring 進行無縫集成。
3.1最簡單的接口發布
這里先不與Spring集成,需要的jar包
asm-3.3.jar
commons-logging-1.1.1.jar
cxf-2.4.2.jar
jetty-continuation-7.4.5.v20110725.jar
jetty-http-7.4.5.v20110725.jar
jetty-io-7.4.5.v20110725.jar
jetty-security-7.4.5.v20110725.jar
jetty-server-7.4.5.v20110725.jar
jetty-util-7.4.5.v20110725.jar
neethi-3.0.1.jar
wsdl4j-1.6.2.jar
xmlschema-core-2.0.jar
這里用的是2.4.2的,老版本,不要介意,最新版本3.1.6,可以自行下載
- @WebService
- //聲明soap1.2
- //@BindingType(value=SOAPBinding.SOAP12HTTP_BINDING)
- public interface LanguageService {
- public String getLanguage(int num);
- }
- //開發語言排行榜
- @WebService
- public class LanguageServiceImpl implements LanguageService {
- @Override
- public String getLanguage(int num) {
- String language="";
- switch (num) {
- case 1:
- language="java語言";
- break;
- case 2:
- language="C語言";
- break;
- case 3:
- language="Objective-C語言";
- break;
- case 4:
- language="C#語言";
- break;
- default:
- language="沒有找到你要排名的語言";
- break;
- }
- return language;
- }
- /*
- * 方式1:ServerFactoryBean
- * 不支持注解,就算你添加了注解也不起作用
- * 所以你不能修改服務名,方法名,參數名,工作空間等
- * 另外不支持攔截器
- * 方式2:JaxWsServerFactoryBean為ServerFactoryBean的子類(功能擴展)
- * 特點:
- * 1、支持注解,如果你沒有使用@webService,運行后,是不會發布方法的。。,所以注解必須加
- * 如果有接口實現,只需要在接口加上注解即可,命名注解也要在接口里面做
- * 這跟之前的純java開發webservice有些不一樣,純java開發接口和實現類要改名必須都加上注解才行
- * 2、支持攔截器
- * 3、生成的wsdl更加規范
- * 兩者的wsdl區別:
- * a.方式1 默認不加servcie 方式2:默認加Service,比如<wsdl:definitions name="LanguageServiceService"
- * b.方式1 元素前綴 xsd 方式2 元素前綴xs
- * c.方式1
- <wsdl:portType name="LanguageServicePortType">
- 方式2
- <wsdl:portType name="LanguageService">沒有加PortType
- webService訪問流程:
- 1.首先進行握手,檢查本地代理與服務端的wsdl是否一致,采用的是get請求驗證
- 2.采用post請求,封裝請求參數到xml中,采用soap通信,將這個請求xml傳輸到服務端
- 3.封裝結果為xml,采用soap通信返回xml,再到客戶端解析得到方法的返回值
- */
- //方式1:不通過注解發布
- //這種方式沒有添加webService注解,也就是說沒有注解也可以發布webService服務,
- // 但是這種方式不是很規范,比如我們不可以通過注解的方式來修改WSDL的標簽信息
- private static void publishServie1(){
- LanguageService languageService=new LanguageServiceImpl();
- ServerFactoryBean bean=new ServerFactoryBean();
- //Endpoint :地址 , 實現對象
- // bean.setAddress("http://192.168.8.178:9999/ws/cxf/languangeService");
- bean.setAddress("http://127.0.0.1:9999/ws/cxf/languangeService?WSDL");
- //注冊服務器地址和端口
- bean.setServiceClass(LanguageService.class);//對外提供webservcie的業務類或者接口
- //注冊哪個實現類提供服務
- bean.setServiceBean(languageService);//服務的實現bean
- Server server = bean.create();//創建,發布webservice
- //10秒有效
- // Thread.sleep(1*10*1000);
- // server.stop();
- // System.exit(0);
- }
- //JaxWsServerFactoryBean是ServerFactoryBean的子類
- //可以提供攔截器
- private static void publishServie2(){
- JaxWsServerFactoryBean bean=new JaxWsServerFactoryBean();
- //地址WSDL加不加都可以,訪問時加上就可以了,不加端口也可以發布
- bean.setAddress("http://127.0.0.1:8888/ws/cxf/languangeService?WSDL");
- // bean.setAddress("http://127.0.0.1/ws/cxf/languangeService?WSDL");
- //設置服務接口
- bean.setServiceClass(LanguageService.class);
- //設置服務的類
- bean.setServiceBean(new LanguageServiceImpl());
- //輸入信息攔截器
- bean.getInInterceptors().add(new LoggingInInterceptor());
- //輸出信息攔截器
- bean.getOutInterceptors().add(new LoggingOutInterceptor());
- Server server = bean.create();
- }
- public static void main(String[] args) throws Exception{
- // publishServie1();
- publishServie2();
- }
- }
3.2 結合Spring發布服務
這里重點是如何配置發布服務,業務邏輯僅僅是演示(輔助),不必參考
要發布的服務:保存和根據姓名查詢員工
saveEmployee findEmployeeByName
步驟:
3.2.1.導包cxf2.7.18里面的所有jar包 ,其中jetty包不需要,因為應用是部署在tomcat里面
3.2. 2.建實體類,DB類(模擬)
- public class Employee {
- private String name;
- private Integer age;
- @DateTimeFormat(pattern="yyyy-MM-dd")
- private Date birthday;
- public Employee() {
- super();
- }
- public Employee(String name, Integer age, Date birthday) {
- super();
- this.name = name;
- this.age = age;
- this.birthday = birthday;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- @Override
- public String toString() {
- return "Employee [name=" + name + ", age=" + age + ", birthday="
- + birthday + "]";
- }
- }
- //運行時如果報錯Unsupported major.minor version 51.0
- //這說明jdk版本不對,即tomcat的jdk版本跟項目版本不一致
- @Repository
- public class EmployeeDB {
- public EmployeeDB(){}
- private static List<Employee> list=new ArrayList<Employee>();
- static{
- list.add(new Employee("小米", 12, new Date()));
- }
- public void save(Employee employee){
- list.add(employee);
- }
- public Employee findByName(String name){
- if (list.size()>0) {
- for(Employee e:list){
- if (e.getName().equals(name)) {
- return e;
- }
- }
- }
- return null;
- }
- public List<Employee> findAll(){
- return list;
- }
- public void deleteByName(String name) throws Exception{
- if (name.length()==0||name==null) {
- throw new Exception("刪除用戶異常");
- }
- for (int i = 0; i < list.size(); i++) {
- if (name.equals(list.get(i).getName())) {
- list.remove(list.get(i));
- break;
- }
- }
- }
- }
3.2.3.建立服務類
- @WebService(serviceName="EmployeeService")
- public interface EmployeeService {
- public void saveEmployee(@WebParam(name="employee")Employee employee );
- public @WebResult(name="Employee")Employee findEmployeeByName(@WebParam(name="name")String name);
- }
- public class EmployeeServiceImpl implements EmployeeService{
- @Autowired
- private EmployeeDB db;
- @Override
- public void saveEmployee(Employee employee) {
- System.out.println("EmployeeDB:"+db);
- db.save(employee);
- }
- @Override
- public Employee findEmployeeByName(String name) {
- if (name.length()==0||name==null) {
- return null;
- }
- return db.findByName(name);
- }
- }
3.2. 4.寫頁面,寫Servlet(如果單純為了測試發布的服務,這些可以不需要)
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>添加員工</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <form action="${pageContext.request.contextPath}/AddServlet" method="post">
- 用戶名:<input type="text" name="name" /><br/>
- 年齡:<input type="text" name="age" /><br/>
- 生日:<input type="text" name="birthday" /><br/>
- <input type="submit" value="提交" />
- </form>
- </body>
- </html>
- public class AddServlet extends HttpServlet {
- private EmployeeService employeeService;
- @Override
- public void init() throws ServletException {
- WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
- //得到的是實現類對象
- employeeService=(EmployeeService) context.getBean("employeeService");
- System.out.println("employeeService:"+employeeService);
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=utf-8");
- String name=request.getParameter("name");
- String age=request.getParameter("age");
- String birthday=request.getParameter("birthday");
- Employee employee=new Employee();
- employee.setName(name);
- employee.setAge(Integer.parseInt(age));
- employee.setBirthday(DateUtil.stringToDate(birthday, "yyyy-MM-dd"));
- employeeService.saveEmployee(employee);
- //檢查是否存進去進去了
- EmployeeDB db=new EmployeeDB();
- System.out.println("db.size():"+db.findAll().size());
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
- }
3.2.5.配置applicationContext.xml和web.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:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <context:component-scan base-package="com.demo.*"></context:component-scan>
- <bean id="employeeService" class="com.demo.webService.EmployeeServiceImpl"></bean>
- <!-- 配置cxf
- 地址: 比如: http://192.168.114.10:8080/CXF_Server/ws/employeeManager
- 組成 : http://192.168.114.10:8080 +CXF_Server( 項目名)+ws(過濾的路徑)+/employeeManager(自定義部分)
- 服務類 :
- 服務的實現類:
- 攔截器
- -->
- <jaxws:server serviceClass="com.demo.webService.EmployeeService" address="/employeeService">
- <jaxws:serviceBean>
- <ref bean="employeeService"/>
- </jaxws:serviceBean>
- <jaxws:inInterceptors>
- <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
- </jaxws:inInterceptors>
- <jaxws:outInterceptors>
- <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
- </jaxws:outInterceptors>
- </jaxws:server>
- </beans>
web.xml
- <?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
- <display-name>CXFDemo1</display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <!-- 配置CXF的Servlet,用來處理webService的請求 -->
- <servlet>
- <servlet-name>cxf</servlet-name>
- <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet>
- <servlet-name>AddServlet</servlet-name>
- <servlet-class>com.demo.web.AddServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>cxf</servlet-name>
- <url-pattern>/ws/*</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>AddServlet</servlet-name>
- <url-pattern>/AddServlet</url-pattern>
- </servlet-mapping>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
- </web-app>
3.2.6.工具類
- public class DateUtil {
- private DateUtil() {
- }
- public static String dateToString(Date d, String format) {
- return new SimpleDateFormat(format).format(d);
- }
- public static Date stringToDate(String s, String format) {
- try {
- return new SimpleDateFormat(format).parse(s);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
3.2.7.訪問:http://localhost:8080/CXFDemo2/ws
4.Axis2發布服務
4.1比較
Axis2是從Axis1.x系列發展而來。CXF則是XFire和Celtix項目的結合產品。Axis2是從底層全部重新實現,使用了新的擴展性更好模塊架構。 CXF也重新的深化了XFire和Celtix這兩個開發工具。
1.CXF支持 WS-Addressing,WS-Policy, WS-RM, WS-Security和WS-I Basic Profile。Axis2不支持WS-Policy,現在應該支持了。
2. CXF可以很好支持Spring。Axis2不能(沒測試過,現在應該可以吧,待測。。)
3. AXIS2支持更廣泛的數據並對,如XMLBeans,JiBX,JaxMe和JaxBRI和它自定義的數據綁定ADB。注意JaxME和JaxBRI都還是試驗性的。CXF只支持JAXB和Aegis。
4. Axis2支持多語言-除了Java,他還支持C/C++版本
如何抉擇:
1、如果應用程序需要多語言的支持,Axis2 應當是首選了;
2、如果應用程序是遵循 Spring 哲學路線的話,Apache CXF 是一種更好的選擇,特別對嵌入式的 Web Services 來說;
目前asix2最新版本1.7.1
4.2安裝插件
4.2.1Eclipse
axis2-eclipse-codegen-plugin-1.7.1.zip
axis2-eclipse-service-plugin-1.7.1.zip
解壓的jar包放入Eclipse的dropins目錄
4.2.2Myeclipse
axis2-eclipse-codegen-wizard.zip
axis2-eclipse-service-archiver-wizard.zip
解壓放入到Myeclipse的dropins目錄
4.3 實現方式有很多
這里簡單說明下最快速的2種方式,Eclipse實現
方式1:不使用插件實現
1.配置環境變量(不是必須的)
2.配置webService環境
2.建立一個動態web項目
方式2:使用插件實現
建一個普通的動態項目,不選擇webService環境
這個實現網上有很多,實現參考:http://blog.csdn.NET/wangchangpen62/article/details/45171001
好了,先寫到這吧
以上做了個簡單的小結,深入應用還待學習。。。。