CXF学习之旅(三) - 使用Maven根据WSDL生成生成Java代码

来源:互联网 发布:SQL中trunc函数 编辑:程序博客网 时间:2024/06/16 16:03

转:

http://blog.csdn.net/yuguiyang1990/article/details/9800851

好久没有看CXF了,最近又一次用到,在这里根据日常使用,接着整理。

好吧,先复习一下,怎样使用Maven创建一个CXF的项目。

1.根据之前的博客,新建一个Web项目

2.修改pom.xml

[html] view plain copy print?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>org.ygy.cxf</groupId>  
  5.   <artifactId>cxf-date</artifactId>  
  6.   <packaging>war</packaging>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <name>cxf-date Maven Webapp</name>  
  9.   <url>http://maven.apache.org</url>  
  10.     
  11.   <!-- 属性配置 -->  
  12.     <properties>  
  13.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  14.         <cxf.version>2.7.5</cxf.version>  
  15.         <spring.version>3.1.1.RELEASE</spring.version>  
  16.     </properties>  
  17.   
  18.     <dependencies>  
  19.         <dependency>  
  20.             <groupId>junit</groupId>  
  21.             <artifactId>junit</artifactId>  
  22.             <version>4.10</version>  
  23.             <scope>test</scope>  
  24.         </dependency>  
  25.   
  26.           
  27.         <dependency>  
  28.             <groupId>org.apache.cxf</groupId>  
  29.             <artifactId>cxf-rt-frontend-jaxws</artifactId>  
  30.             <version>${cxf.version}</version>  
  31.         </dependency>  
  32.   
  33.         <dependency>  
  34.             <groupId>org.apache.cxf</groupId>  
  35.             <artifactId>cxf-rt-transports-http</artifactId>  
  36.             <version>${cxf.version}</version>  
  37.         </dependency>  
  38.   
  39.         <dependency>  
  40.             <groupId>org.apache.cxf</groupId>  
  41.             <artifactId>cxf-rt-transports-http-jetty</artifactId>  
  42.             <version>${cxf.version}</version>  
  43.         </dependency>  
  44.   
  45.         <dependency>  
  46.             <groupId>org.springframework</groupId>  
  47.             <artifactId>spring-context</artifactId>  
  48.             <version>${spring.version}</version>  
  49.         </dependency>  
  50.   
  51.         <dependency>  
  52.             <groupId>org.springframework</groupId>  
  53.             <artifactId>spring-web</artifactId>  
  54.             <version>${spring.version}</version>  
  55.         </dependency>  
  56.   
  57.         <dependency>  
  58.             <groupId>org.eclipse.jetty</groupId>  
  59.             <artifactId>jetty-webapp</artifactId>  
  60.             <version>8.1.11.v20130520</version>  
  61.         </dependency>  
  62.   
  63.     </dependencies>  
  64.   
  65.     <build>  
  66.         <finalName>cxf-date</finalName>  
  67.     </build>  
  68. </project>  

 

3.修改web.xml

[html] view plain copy print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.            
  7.     <context-param>  
  8.         <param-name>contextConfigLocation</param-name>  
  9.         <param-value>classpath:applicationContext.xml</param-value>  
  10.     </context-param>  
  11.   
  12.    <listener>  
  13.       <listener-class>  
  14.          org.springframework.web.context.ContextLoaderListener  
  15.       </listener-class>  
  16.    </listener>  
  17.      
  18.      
  19.    <servlet>    
  20.         <servlet-name>CXFServlet</servlet-name>    
  21.             <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    
  22.         <load-on-startup>1</load-on-startup>    
  23.     </servlet>    
  24.     <servlet-mapping>    
  25.         <servlet-name>CXFServlet</servlet-name>    
  26.         <url-pattern>/services/*</url-pattern>    
  27.     </servlet-mapping>    
  28.      
  29.   
  30. </web-app>  

 

4.写服务接口

[java] view plain copy print?
  1. package org.ygy.cxf.entity;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. public class BookEntity implements Serializable {  
  7.     private static final long serialVersionUID = -860948461727285535L;  
  8.   
  9.     private Integer id;  
  10.     private String name;  
  11.     private String author;  
  12.     private Date publicationDate;//出版日期  
  13.   
  14.     public Integer getId() {  
  15.         return id;  
  16.     }  
  17.   
  18.     public void setId(Integer id) {  
  19.         this.id = id;  
  20.     }  
  21.   
  22.     public String getName() {  
  23.         return name;  
  24.     }  
  25.   
  26.     public void setName(String name) {  
  27.         this.name = name;  
  28.     }  
  29.   
  30.     public String getAuthor() {  
  31.         return author;  
  32.     }  
  33.   
  34.     public void setAuthor(String author) {  
  35.         this.author = author;  
  36.     }  
  37.   
  38.     public Date getPublicationDate() {  
  39.         return publicationDate;  
  40.     }  
  41.   
  42.     public void setPublicationDate(Date publicationDate) {  
  43.         this.publicationDate = publicationDate;  
  44.     }  
  45.   
  46. }  


 

[java] view plain copy print?
  1. package org.ygy.cxf.service;  
  2.   
  3. import javax.jws.WebParam;  
  4. import javax.jws.WebService;  
  5.   
  6. import org.ygy.cxf.entity.BookEntity;  
  7.   
  8. @WebService  
  9. public interface IBookService {  
  10.     /** 
  11.      * 添加一本书 
  12.      * @param book 
  13.      */  
  14.     public void add(@WebParam(name="book")BookEntity book);  
  15.       
  16.     /** 
  17.      * 根据ID,查询书籍 
  18.      * @param id 
  19.      * @return 
  20.      */  
  21.     public BookEntity query(@WebParam(name="id")Integer id);  
  22. }  


 

[java] view plain copy print?
  1. package org.ygy.cxf.service.impl;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import javax.jws.WebService;  
  6.   
  7. import org.ygy.cxf.entity.BookEntity;  
  8. import org.ygy.cxf.service.IBookService;  
  9. import org.ygy.cxf.util.DateUtil;  
  10.   
  11. @WebService(endpointInterface="org.ygy.cxf.service.IBookService")  
  12. public class BookService implements IBookService {  
  13.   
  14.     @Override  
  15.     public void add(BookEntity book) {  
  16.         System.out.println("id->" + book.getId());  
  17.         System.out.println("name->" + book.getName());  
  18.         System.out.println("author->" + book.getAuthor());  
  19.         System.out.println("publicationDate->" + DateUtil.toStr(book.getPublicationDate()));  
  20.     }  
  21.   
  22.     @Override  
  23.     public BookEntity query(Integer id) {  
  24.         BookEntity book = new BookEntity();  
  25.         book.setId(1001);  
  26.         book.setName("OnePiece");  
  27.         book.setAuthor("YGY");  
  28.         book.setPublicationDate(new Date());  
  29.           
  30.         return book;  
  31.     }  
  32.   
  33. }  


 

[java] view plain copy print?
  1. package org.ygy.cxf.util;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Date;  
  5.   
  6. public class DateUtil {  
  7.     private static SimpleDateFormat sdf = null;  
  8.       
  9.     private DateUtil() {  
  10.           
  11.     }  
  12.       
  13.     /** 
  14.      * 将日期转换成字符串 
  15.      * @param date 日期 
  16.      * @return 
  17.      */  
  18.     public static String toStr(Date date) {  
  19.         sdf = new SimpleDateFormat(Configs.COMMON);  
  20.           
  21.         return sdf.format(date);  
  22.     }  
  23. }  


 

[java] view plain copy print?
  1. package org.ygy.cxf.util;  
  2.   
  3. public class Configs {  
  4.     public static final String COMMON = "yyyy-MM-dd HH:mm:ss";  
  5. }  


 

5.新建Spring配置文件

[html] view plain copy print?
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  4.     xsi:schemaLocation="  
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  6. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  7.   
  8.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  9.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  10.   
  11.     <jaxws:endpoint  
  12.         id="bookService"  
  13.         implementor="org.ygy.cxf.service.impl.BookService"  
  14.         address="/BookService"  
  15.     ></jaxws:endpoint>  
  16.         
  17. </beans>  


 

6.好了,贴了这么多代码,发布项目测试一下先

7.到这里,都算是复习了,在以前,如果想要根据WSDL生成客户端代码的话,可能要使用命令行wsdl2java命令,还挺长的,

现在,使用了Maven,就简单多了。

修改pom.xml

[html] view plain copy print?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>org.ygy.cxf</groupId>  
  5.   <artifactId>cxf-date</artifactId>  
  6.   <packaging>war</packaging>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <name>cxf-date Maven Webapp</name>  
  9.   <url>http://maven.apache.org</url>  
  10.     
  11.   <!-- 属性配置 -->  
  12.     <properties>  
  13.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  14.         <cxf.version>2.7.5</cxf.version>  
  15.         <spring.version>3.1.1.RELEASE</spring.version>  
  16.     </properties>  
  17.   
  18.     <dependencies>  
  19.         <dependency>  
  20.             <groupId>junit</groupId>  
  21.             <artifactId>junit</artifactId>  
  22.             <version>4.10</version>  
  23.             <scope>test</scope>  
  24.         </dependency>  
  25.   
  26.           
  27.         <dependency>  
  28.             <groupId>org.apache.cxf</groupId>  
  29.             <artifactId>cxf-rt-frontend-jaxws</artifactId>  
  30.             <version>${cxf.version}</version>  
  31.         </dependency>  
  32.   
  33.         <dependency>  
  34.             <groupId>org.apache.cxf</groupId>  
  35.             <artifactId>cxf-rt-transports-http</artifactId>  
  36.             <version>${cxf.version}</version>  
  37.         </dependency>  
  38.   
  39.         <dependency>  
  40.             <groupId>org.apache.cxf</groupId>  
  41.             <artifactId>cxf-rt-transports-http-jetty</artifactId>  
  42.             <version>${cxf.version}</version>  
  43.         </dependency>  
  44.   
  45.         <dependency>  
  46.             <groupId>org.springframework</groupId>  
  47.             <artifactId>spring-context</artifactId>  
  48.             <version>${spring.version}</version>  
  49.         </dependency>  
  50.   
  51.         <dependency>  
  52.             <groupId>org.springframework</groupId>  
  53.             <artifactId>spring-web</artifactId>  
  54.             <version>${spring.version}</version>  
  55.         </dependency>  
  56.   
  57.         <dependency>  
  58.             <groupId>org.eclipse.jetty</groupId>  
  59.             <artifactId>jetty-webapp</artifactId>  
  60.             <version>8.1.11.v20130520</version>  
  61.         </dependency>  
  62.   
  63.     </dependencies>  
  64.   
  65.     <build>  
  66.         <finalName>cxf-date</finalName>  
  67.           
  68.         <plugins>  
  69.             <plugin>  
  70.                 <groupId>org.apache.cxf</groupId>  
  71.                 <artifactId>cxf-codegen-plugin</artifactId>  
  72.                 <version>${cxf.version}</version>  
  73.                 <executions>  
  74.                     <execution>  
  75.                         <id>generate-sources</id>  
  76.                         <phase>generate-sources</phase>  
  77.                         <configuration>  
  78.                             <sourceRoot>src/main/resources/cxf</sourceRoot>  
  79.                             <wsdlOptions>  
  80.                                 <wsdlOption>  
  81.                                     <wsdl>http://localhost:8080/cxf-date/services/BookService?wsdl</wsdl>  
  82.                                     <frontEnd>jaxws21</frontEnd>  
  83.                                     <faultSerialVersionUID>1</faultSerialVersionUID>  
  84.                                 </wsdlOption>  
  85.                             </wsdlOptions>  
  86.                         </configuration>  
  87.                         <goals>  
  88.                             <goal>wsdl2java</goal>  
  89.                         </goals>  
  90.                     </execution>  
  91.                 </executions>  
  92.             </plugin>  
  93.         </plugins>  
  94.     </build>  
  95. </project>  

这里,添加了一个插件,通过它,可以根据配置的WSDL地址,将代码生成到指定的文件夹

sourceRoot:就是生成文件存放的位置

wsdl:就是WSDL地址

然后,在pom.xml上右键单击,选择run as 下的mvn generate-sources

然后,刷新一下项目,就会看到生成的代码:

好了,先写到这,主要介绍一下使用Maven根据WSDL自动生成Java代码。

 

0 0
原创粉丝点击