CXF框架快速应用

来源:互联网 发布:淘宝线上推广合同范本 编辑:程序博客网 时间:2024/05/21 13:56
  • maven依赖jar包

      <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-core</artifactId>    <version>${cxf-version}</version></dependency>
  • Spring配置
    applicationContext.xml

  <!-- 设置spring的环境 -->  <context-param>    <!--contextConfigLocation是不能修改的  -->    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext.xml</param-value>  </context-param>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>

web.xml

    <!--Spring路径 -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>/WEB-INF/applicationContext.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>
  • CXF发布SOAP协议的服务
    web.xml
  <!-- 配置CXF --><servlet>    <servlet-name>CXF</servlet-name>    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class></servlet><servlet-mapping>    <servlet-name>CXF</servlet-name>    <url-pattern>/ws/*</url-pattern></servlet-mapping> 

配置applicationContest.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"    xmlns:jaxrs="http://cxf.apache.org/jaxrs"     xmlns:cxf="http://cxf.apache.org/core"    xsi:schemaLocation="http://www.springframework.org/schema/beans                             http://www.springframework.org/schema/beans/spring-beans.xsd                            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd                            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd                            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">    <!-- <jaxws:server发布SOAP协议的服务 ,对JaxWsServerFactoryBean类封装-->    <jaxws:server address="/weather" serviceClass="cc.ws.server.WeatherService">        <jaxws:serviceBean>            <ref bean="weatherServiceImpl"/>        </jaxws:serviceBean>    </jaxws:server>    <!-- 配置服务实现类 -->    <bean name="weatherServiceImpl" class="cc.ws.server.WeatherServiceImpl"></bean>        <!-- <jaxws:client实现客户端 ,对JaxWsProxyFactoryBean类封装-->    <jaxws:client id="weatherClient"     address="http://192.168.211.129:8080/ws_cxf_spring_server-0.0.1-SNAPSHOT/ws/weather"    serviceClass="cc.ws.server.WeatherService"     /></beans>

服务端@WebService
客户端

ApplicationContext context = new ClassPathXmlApplicationContext("/WEB-INF/applicationContext.xml");        WeatherService weatherService = (WeatherService) context.getBean("weatherClient");        String result = weatherService.queryWeather("广州");

  • CXF发布REST的服务
    pojo类:
    @XmlRootElement(name=”student”)//@XmlRootElement可以实现对象和XML数据之间的转换
    SEI接口:
    @WebService
    @Path(“/Student”)
    抽象方法:
    @GET//指定请求方式,如果服务端发布的时候指定的是GET(POST),那么客户端访问时必须使用GET(POST)
    @Produces(MediaType.APPLICATION_XML)//指定服务数据类型
    @Path(“/query/{id}”)//@Path(“/query/{id}”)就是将“/query”映射到方法上,“{id}”映射到参数上,多个参数,以“/”隔开,放到“{}”中
    public Student query(@PathParam(“id”)long id);
    配置applicationContext.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"    xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"    xsi:schemaLocation="http://www.springframework.org/schema/beans                             http://www.springframework.org/schema/beans/spring-beans.xsd                            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd                            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd                            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"><!-- <jaxrs:server发布REST的服务 ,对JAXRSServerFactoryBean类封装-->      <jaxrs:server address="/user">        <jaxrs:serviceBeans>            <ref bean="studentInterface"/>        </jaxrs:serviceBeans>    </jaxrs:server>    <!-- 配置服务实现类 -->    <bean name="studentInterface" class="cn.itcast.ws.rest.server.StudentInterfaceImpl"/></beans>

配置web.xml

  <!-- 配置CXF的Servlet -->  <servlet>    <servlet-name>CXF</servlet-name>    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>CXF</servlet-name>    <url-pattern>/ws/*</url-pattern>  </servlet-mapping>

ps:
获取wsdl使用
wsdl2java -d 存放java文件地址 wsdl地址

0 0
原创粉丝点击