JAXWS CXF Spring + MyEclipse + Maven + Tomcat Byron自學視頻02
Description: 使用 Apache CXF 建置 WebService 並 Deploy 到 Tomcat範例來源: http://cxf.apache.org/docs/writing-a-service-with-spring.htmlTags: jaxws, cxf, apache, webservice, myeclipse, spring, jetty, qname, maven, hello world, java, eclipse, tomcat,byron
Powered by VideoBam - Free Video Hosting
提及技巧:
* MyEclipse 中, Java Project 轉成 Web Project 解法.
* Maven test 時, 使測試程式有效.* Maven test 時, maven-surefire-plugin 中文亂碼解法.
pom.xml
<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.wyd.jaxws</groupId> <artifactId>jaxws-cxf-web</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>Simple CXF project using spring configuration</name> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.3.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.3.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <argLine>-Dfile.encoding=UTF-8</argLine> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
client-beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <jaxws:client id="helloClient" serviceClass="com.wyd.cxf.HelloWorld" address="http://localhost:8080/jaxws-cxf-web/HelloWorld" /> </beans>
ClientTest.java
package junit.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wyd.cxf.HelloWorld; public class ClientTest { public static void main(String args[]) throws Exception { // START SNIPPET: client ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "client-beans.xml" }); HelloWorld client = (HelloWorld) context.getBean("helloClient"); String response = client.sayHi(args[0]); System.out.println("Response: " + response); System.exit(0); // END SNIPPET: client } @Test public void MyTest() throws Exception { ClientTest.main(new String[] { "王五" }); } }