CXF入門教程(1) -- 第一個webService
參考:[url]http://blog.csdn.net/neareast/article/details/7714778[/url]
本程式碼使用Maven來管理依賴
[b]pom.xml[/b]
[b]javaBean[/b]
[b]介面[/b]:首先我們寫一個serivce介面,其中包含一個操作sayHi,它的作用是向其提交名字的人問好。
[b]實現類[/b]: WebParam註解是必須的,因為java藉口編譯後的.class檔案不儲存引數的名字,所以如果沒有加註解,引數將被命名為arg0。介面實現部分的示例如下:
@WebService註解讓CXF知道我們希望使用哪個介面來建立WSDL,本例中就是iHelloWorld介面。
[b]服務釋出類[/b]
進入: [url]http://localhost:8080/helloWorld?wsdl[/url]
This XML file does not appear to have any style information associated
with it. The document tree is shown below.
本程式碼使用Maven來管理依賴
[b]pom.xml[/b]
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pandy</groupId>
<artifactId>webservice</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>webservice Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.2.1.RELEASE</spring.version>
<tomcat.version>2.1-SNAPSHOT</tomcat.version>
<junit.version>3.8.1</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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>
<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-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>2.7.3</version>
</dependency>
</dependencies>
<build>
<finalName>webservice</finalName>
</build>
</project>
[b]javaBean[/b]
package com.webservices.bean;
public class User {
private String name;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [userName=" + name + ", email=" + email + "]";
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
[b]介面[/b]:首先我們寫一個serivce介面,其中包含一個操作sayHi,它的作用是向其提交名字的人問好。
package com.webservices;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.webservices.bean.User;
/**
* 將要用來發布的介面
* @author pandy
*
*/
@SuppressWarnings("restriction")
@WebService
public interface iHelloWorld {
// 加入WebParam註解,以保證xml檔案中引數名字的正確性
String sayHi(@WebParam(name = "text") String text);
String sayHiToUser(User user);
}
[b]實現類[/b]: WebParam註解是必須的,因為java藉口編譯後的.class檔案不儲存引數的名字,所以如果沒有加註解,引數將被命名為arg0。介面實現部分的示例如下:
package com.webservices.impl;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.jws.WebService;
import com.webservices.iHelloWorld;
import com.webservices.bean.User;
/**
* 介面的實現類
* @author pandy
*
*/
@SuppressWarnings({ "restriction" })
@WebService(endpointInterface = "com.webservices.iHelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements iHelloWorld {
Map<Integer, User> users = new LinkedHashMap<Integer, User>();
@Override
public String sayHi(String text) {
System.out.println("sayHi called");
return "Hello " + text;
}
@Override
public String sayHiToUser(User user) {
System.out.println("sayHiToUser called");
users.put(users.size() + 1, user);
return "Hello " + user.getName();
}
}
@WebService註解讓CXF知道我們希望使用哪個介面來建立WSDL,本例中就是iHelloWorld介面。
[b]服務釋出類[/b]
import javax.xml.ws.Endpoint;
import com.webservices.impl.HelloWorldImpl;
/**
* 釋出程式
* @author pandy
*
*/
@SuppressWarnings("restriction")
public class ServiceTest {
protected ServiceTest() throws Exception {
// START SNIPPET: publish
System.out.println("Starting Server");
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:8080/helloWorld";
Endpoint.publish(address, implementor);
// END SNIPPET: publish
}
public static void main(String args[]) throws Exception {
new ServiceTest();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
進入: [url]http://localhost:8080/helloWorld?wsdl[/url]
This XML file does not appear to have any style information associated
with it. The document tree is shown below.
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is
JAX-WS RI 2.2.4-b01. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is
JAX-WS RI 2.2.4-b01. -->
<definitions
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"
xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://impl.webservices.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.webservices.com/"
name="HelloWorld">
<import namespace="http://webservices.com/" location="http://localhost:8080/helloWorld?wsdl=1" />
<binding xmlns:ns1="http://webservices.com/" name="HelloWorldImplPortBinding"
type="ns1:iHelloWorld">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="document" />
<operation name="sayHi">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
<operation name="sayHiToUser">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
</binding>
<service name="HelloWorld">
<port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding">
<soap:address location="http://localhost:8080/helloWorld" />
</port>
</service>
</definitions>
相關文章
- CXF入門教程(5) -- webService非同步呼叫模式Web非同步模式
- CXF入門教程(2) -- 第一個客戶端客戶端
- CXF入門教程(3) -- webService客戶端開發步驟詳解Web客戶端
- CXF--入門
- WebService 簡單入門教程(Java Web專案)WebJava
- CXF實現webService服務(一)Web
- 1(1)FPGA入門第一關FPGA
- (1)入門MasaFramework教程Framework
- WebService之Spring+CXF整合示例WebSpring
- cxf設定代理訪問webservice介面Web
- go語言入門教程分享:第一個程式:HelloWorldGo
- Flutter入門教程(四)第一個flutter專案解析Flutter
- CXF入門教程(4) -- 設定上下文連線屬性
- OPNET入門1-第一個模擬例子
- cxf WebService設定wsdl中soapAction的值Web
- CXF建立webservice客戶端和服務端Web客戶端服務端
- 使用CXF與Spring整合實現RESTFul WebServiceSpringRESTWeb
- maven+CXF+Spring+tomcat 開發webserviceMavenSpringTomcatWeb
- ROS入門教程歸納1ROS
- Redux 入門教程(1):基本用法Redux
- Xcode Server 教程1:入門XCodeServer
- 使用apache CXF開發第一個Web服務ApacheWeb
- Spring整合CXF,釋出RSETful 風格WebServiceSpringWeb
- WebService框架大比較(Axis,axis2,Xfire以及cxf)Web框架
- webservice快速入門-SOAP和WSDL(三)Web
- java webservice開發入門之一JavaWeb
- java webservice開發入門之二JavaWeb
- ASP入門教程 1小時ASP入門,非常簡單
- 使用瀏覽器位址列呼叫CXF Webservice的寫法瀏覽器Web
- 開發:隨筆記錄之 CXF 動態呼叫Webservice筆記Web
- 用cxf編寫基於spring的webservice之下篇SpringWeb
- Aseprite入門:第一個gif動圖
- Mybatis入門及第一個Mybatis程式MyBatis
- shell入門--第一個shell指令碼指令碼
- vue-element-admin入門教程(1)Vue
- 【Laravel 入門教程】學習筆記 1Laravel筆記
- UWP入門教程1——UWP的前世今生
- IOS 初級開發入門教程(二)第一個HelloWorld工程及StoryBoard使用iOS