1. 程式人生 > >基於spring註解和xml配置的webService

基於spring註解和xml配置的webService

1.需要的jar包

commons-logging-1.1.1.jar
cxf-2.4.3.jar
neethi-3.0.1.jar
spring-aop-3.0.5.RELEASE.jar
spring-asm-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-context-support-3.0.5.RELEASE.jar
spring-core-3.0.5.RELEASE.jar
spring-expression-3.0.5.RELEASE.jar
spring-web-3.0.5.RELEASE.jar
wsdl4j-1.6.2.jar
xmlschema-core-2.0.1.jar

2.有關WebService的註解

註解介面類的:@WebService 表明這個介面類是用於WebService用的。
註解方法引數的:@WebParam(name = "name") name="name"。表示引數名稱為name(雙引號裡面的值為引數名稱)
註解方法的:@WebMethod(operationName="sayHello5") 告知該方法是用於WebService用的。operationName="sayHello5"表示方法的方法名為sayHello5
註解方法返回值的:@WebResult(name = "result") (name = "result")告知方法的返回值變數為result


3.例子

3.1新建一個介面來提供webService服務

@WebService
public interface Test1 {

public String sayHello();

public void sayHello2(@WebParam(name = "name") String name);

public String sayHello3(@WebParam(name = "name") String name);

@WebMethod(operationName="sayHello4")
public void sayHello4(@WebParam(name = "name") String name);

@WebMethod(operationName="sayHello5")
@WebResult(name = "result")
public String sayHello5(@WebParam(name = "name") String name);

}

3.2介面的實現

@WebService(endpointInterface = "com.zbeninfo.webservice.Test1")
@Component//這個是spring的註解
public class Test1Impl implements Test1{

@Override
public String sayHello() {
return "say hello";
}

@Override
public void sayHello2(String name) {
System.out.println("hello," + name);
}

@Override
public String sayHello3(String name) {
return "test:"+name;
}

@Override
public void sayHello4(String name) {
System.out.println("呼叫webservice的insert方法成功,引數:name="+name);
}

@Override
public String sayHello5(String name) {
return "sayHello5:"+name;
}

}

介面實現類也用@WebService來進行註解。裡面有endpointInterface = "com.zbeninfo.webservice.Test1"。endpointInterface是指明webservice的介面。接下來就是實現該介面的方法。普通的java方法。
還有一個註解:@Component。該註解主要用於在不確定該類是什麼型別的情況下(controller、service、dao)。用@Component註解一個類,即等於在spring的配置檔案中設定了定義了bean

3.3建立webservice的配置檔案了(放在src下,和普通的spring配置檔案放在一起即可):applicationContext-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" />

<jaxws:endpoint id="test1" implementor="com.zbeninfo.webservice.impl.Test1Impl" address="/test1"/>
<jaxws:endpointid= "greetingService" implementor="com.gary.test.ws.service.impl.GreetingServiceImpl" address="/GreetingService"/>
</beans>

配置檔案說明:
beans的名稱空間不做過多的解釋,照著新增就行。
主要解釋jaxws:endpoint標籤。
jaxws:endpoint標籤用於設定和釋出WebService類用的。
id表示id屬性,
implementor表示該WebService介面的具體實現類。
address表示用於WebService用於釋出的地址。

3.4在web.xml中需要配置cxf的相關配置

首先新增載入有關WebService的配置檔案
如:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext-*.xml</param-value>
</context-param>

載入CXF的相關配置:
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
表示凡是/ws/*的路徑都是WebService的地址。