cxf 开发webservice 的两种实现方式

网上有许多开发webservice 的博客,不知道咋的我就是看不懂,所以在本人的努力下写这篇博客,希望对需要的人有用,还有许多人弄个破helloworld 程序收人金币本人黑恼火,所以发表这篇播客,cxf-servlet等配置在高版本的cxf中不需要引入,cxf有很多jar 包我也不知道干什么用的,所以这些jar 包是我根据别人的成果尝试后一步步试出来的,还有许多鬼儿子的弄个破jar 包在网上买,所以我打算用maven来搭建项目,学习的路是艰难的,现在网上许多博客好像和你打游击一样,谁也有是新手的时候,摸不着东西时候学习是痛苦的,我最恨的是妈的收保护费弄个破文档质量不知道咋样在网上卖,无耻!,虽然我没有多少C币。

首先需要了解一一下这两个基本的知识

JAX-WS:全称是JavaTM API forXML-Based Web Services
JAX-RS :全称是 JavaTM API forRESTful Web Services

搭建环境使用maven 导入jar包和配置web.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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.huawei.it</groupId>
	<artifactId>cxf</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>cxf</name>
	<description>cxf</description>
	<properties>
		<junit.version>4.9</junit.version>
		<spring.version>4.3.14.RELEASE</spring.version>
		<mybatis.version>3.2.8</mybatis.version>
		<mybatis.spring.version>1.2.2</mybatis.spring.version>
		<slf4j.version>1.6.4</slf4j.version>
		<druid.version>1.0.9</druid.version>
		<servlet-api.version>2.5</servlet-api.version>
		<jsp-api.version>2.0</jsp-api.version>
		<commons-lang3.version>3.3.2</commons-lang3.version>
		<commons-io.version>1.3.2</commons-io.version>
		<cxf.version>3.2.2</cxf.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>${cxf.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>${cxf.version}</version>
		</dependency>
		<dependency>
    		<groupId>org.apache.cxf</groupId>
    		<artifactId>cxf-rt-transports-http</artifactId>
    		<version>${cxf.version}</version>
		</dependency>
		<dependency>
    		<groupId>org.apache.cxf</groupId>
    		<artifactId>cxf-rt-transports-udp</artifactId>
    		<version>${cxf.version}</version>
		</dependency>
		<dependency>
    		<groupId>org.apache.cxf</groupId>
    		<artifactId>cxf-rt-databinding-jaxb</artifactId>
    		<version>${cxf.version}</version>
		</dependency>
		<dependency>
    		<groupId>org.apache.cxf</groupId>
    		<artifactId>cxf-rt-frontend-simple</artifactId>
    		<version>${cxf.version}</version>
		</dependency>
		<dependency>
    		<groupId>org.apache.cxf</groupId>
    		<artifactId>cxf-rt-frontend-jaxws</artifactId>
    		<version>${cxf.version}</version>
		</dependency>
		<dependency>
    		<groupId>org.apache.cxf</groupId>
    		<artifactId>cxf-rt-ws-addr</artifactId>
    		<version>${cxf.version}</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-bindings-soap -->
		<dependency>
    		<groupId>org.apache.cxf</groupId>
    		<artifactId>cxf-rt-bindings-soap</artifactId>
    		<version>${cxf.version}</version>
		</dependency>
		<dependency>
		    <groupId>javax.ws.rs</groupId>
		    <artifactId>javax.ws.rs-api</artifactId>
		    <version>2.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.codehaus.jettison/jettison -->
		<dependency>
		    <groupId>org.codehaus.jettison</groupId>
		    <artifactId>jettison</artifactId>
		    <version>1.3.8</version>
		</dependency>
		<dependency>
		    <groupId>org.apache.cxf</groupId>
		    <artifactId>cxf-rt-frontend-jaxrs</artifactId>
		    <version>${cxf.version}</version>
		</dependency>
		<dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-frontend-jaxws</artifactId>  
            <version>${cxf.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>  
            <version>${cxf.version}</version>  
        </dependency> 
    	 <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-security-jose-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-extension-providers</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-security-sso-oidc</artifactId>
            <version>${cxf.version}</version>
        </dependency> 
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
			<version>${junit.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</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-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</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-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-test</artifactId>
			<version>4.3.3.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-messaging</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<!-- Apache工具组件 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>${commons-lang3.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-io</artifactId>
			<version>${commons-io.version}</version>
		</dependency>
		<!-- 单元测试 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>
		<!-- 日志处理 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<!-- Mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>${mybatis.version}</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>${mybatis.spring.version}</version>
		</dependency>
		<!-- 连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>${druid.version}</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>${servlet-api.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
			<version>${jsp-api.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc6</artifactId>
			<version>11.2.0.1.0</version>
		</dependency>
		
    	
	</dependencies>
	<build>
		<!--配置资源拷贝 -->
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>
			<!-- 如果配置了拷贝java classpath下面的xml,项目中其他的配置文件应该手动配置复制过去 -->
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.xml</include>
					<include>**/*.properties</include>
				</includes>
			</resource>
		</resources>
		<plugins>
			<!-- 资源文件拷贝插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.7</version>
				<configuration>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- java编译插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- 配置tomcat 插件 -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<path>/</path>
					<port>8082</port>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <servlet>
  	<servlet-name>cxfServlet</servlet-name>
  	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>cxfServlet</servlet-name>
  	<url-pattern>/ws/*</url-pattern>
  </servlet-mapping>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

配置spring 的核心配置文件

<?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-2.5.xsd
	http://cxf.apache.org/jaxws 
	http://cxf.apache.org/schemas/jaxws.xsd">
	<bean id="studentServiceImpl" class="com.huawei.it.service.StudentServiceImpl"></bean>
	<jaxws:endpoint id="studentService" implementor="#studentServiceImpl" address="/studentService" />
</beans>

1.创建接口就是在接口上面添加个@WebService

2.发布服务

<?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-2.5.xsd
	http://cxf.apache.org/jaxws 
	http://cxf.apache.org/schemas/jaxws.xsd">
	<bean id="studentServiceImpl" class="com.huawei.it.service.StudentServiceImpl"></bean>
	<jaxws:endpoint id="studentService" implementor="#studentServiceImpl" address="/studentService" />
</beans>

3.生成客户端代码,在path 中加入cxf bin目录的路径,-d 后面的是客户端生成的地址,后面wsdl文件的地址

wsdl2java -d ./test -client http://localhost:8082/ws/studentService?wsdl

4.进行调用,有个client的java文件,直接runAs 直接搞定

CXF开发Restful 服务

1.创建接口

public interface StudentService{
	@GET
	@Path(value = "/selectStudent")
	@Produces(MediaType.APPLICATION_JSON)
	List<StudentVO> selectStudent();
	
	@GET
	@Path(value ="/selectStudentBysNo")
	@Produces(MediaType.APPLICATION_JSON)	
	@Consumes({"application/json","application/xml"})
	StudentVO selectStudentVOBysNo(@QueryParam("sNo") String sNo);
	
	@GET
	@Path(value ="/selectStudentBysNo2/{sNo}")
	@Produces(MediaType.APPLICATION_JSON)	
	@Consumes({"application/json","application/xml"})
	StudentVO selectStudentVOBysNo2(@PathParam("sNo") String sNo);
}

2.发布服务

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:cxf="http://cxf.apache.org/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
    http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security.xsd">
    <jaxrs:server id="studentService" address="/studentService">
        <jaxrs:serviceBeans>
            <bean class="com.huawei.it.service.StudentServiceImpl" />
        </jaxrs:serviceBeans>
           <!-- 支持的协议 -->
        <jaxrs:extensionMappings> 
            <entry key="json" value="application/json" /> 
            <entry key="xml"  value="application/xml" /> 
        </jaxrs:extensionMappings> 
        <!-- 编码格式 -->
        <jaxrs:languageMappings>
               <entry key="en" value="en-gb"/>
        </jaxrs:languageMappings>
        <jaxrs:providers><!--特别注意2.5版本的是JSONProvider  而3.0版本后好像找不到这个类
        	<bean class="org.apache.cxf.jaxrs.provider.json.JsonMapObjectProvider"></bean>
        </jaxrs:providers>
    </jaxrs:server>

3.调用restful服务,rest服务是可以直接在浏览器中直接调用,但是请注意使用@PathParam和@QueryParam时候如果查询没有结果状态码是204,在调用时候不会出现第一种那样的wsdl ,这两个实现采用了不同的标准。

猜你喜欢

转载自blog.csdn.net/x17809211858/article/details/80511860