zoukankan      html  css  js  c++  java
  • Spring整合CXF实现基于Restful风格的webservice

    服务端:

    pom.xml

     <dependencies>
            <!-- cxf 进行rs开发 必须导入  -->
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-frontend-jaxrs</artifactId>
                <version>3.0.1</version>
            </dependency>
            <!-- 日志引入  -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.7.12</version>
            </dependency>
    
            <!-- 客户端 -->
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-rs-client</artifactId>
                <version>3.0.1</version>
            </dependency>
    
            <!-- 扩展json提供者 -->
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-rs-extension-providers</artifactId>
                <version>3.0.1</version>
            </dependency>
    
            <!-- 转换json工具包,被extension providers 依赖 -->
            <dependency>
                <groupId>org.codehaus.jettison</groupId>
                <artifactId>jettison</artifactId>
                <version>1.3.7</version>
            </dependency>
    
            <!-- spring 核心 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.2.4.RELEASE</version>
            </dependency>
            <!-- spring web集成 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>4.2.4.RELEASE</version>
            </dependency>
            <!-- spring 整合junit  -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>4.2.4.RELEASE</version>
            </dependency>
            <!-- junit 开发包 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                        <showWarnings>true</showWarnings>
                    </configuration>
                </plugin>
                <!-- 运行tomcat7方法:tomcat7:run -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <!-- 指定端口 -->
                        <port>8080</port>
                        <!-- 请求路径 -->
                        <path>/</path>
                    </configuration>
                </plugin>
            </plugins>
        </build>

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
             version="2.5">
      <display-name>Archetype Created Web Application</display-name>
      <!--1. cxfsevlet配置-->
      <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>
      <!--2.spring容器配置-->
      <context-param>
        <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>
    
      <!-- 欢迎页面配置 -->
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    <?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:jaxrs="http://cxf.apache.org/jaxrs"
           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">
    
            <!--
                Spring整合cxf发布基于restful风格的服务,关键点:
                1. 服务地址
                2. 服务类
                服务完整访问地址:
            -->
            <jaxrs:server address="/userService">
                    <jaxrs:serviceBeans>
                        <bean class="com.topcheer.service.UserServiceImpl"></bean>
                    </jaxrs:serviceBeans>
    
            </jaxrs:server>
    
    
    </beans>

    注:实体类和服务类和上一篇博客中一样,这里就不贴出来了。

    https://www.cnblogs.com/dalianpai/p/12349801.html

     启动整个服务端就可以了。

    客户端:

    和上一变博客一样,主要就是服务端不一样,直接发请求就可以了。

    直接复制过来

    pom.xml

    复制代码
     <dependencies>
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-frontend-jaxrs</artifactId>
                <version>3.0.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-transports-http-jetty</artifactId>
                <version>3.0.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.7.12</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-rs-client</artifactId>
                <version>3.0.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-rs-extension-providers</artifactId>
                <version>3.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.codehaus.jettison</groupId>
                <artifactId>jettison</artifactId>
                <version>1.3.7</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.10</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                        <showWarnings>true</showWarnings>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    复制代码

    实体类:

    和服务端一样,这里就不贴出来了。

    测试类:

    复制代码
    @Test
        public void testSave(){
            User user = new User();
            user.setId(100);
            user.setUsername("Jerry");
            user.setCity("gz");
            // 通过WebClient对象远程调用服务端
            WebClient
                        .create("http://localhost:8001/ws/userService/user")
                        .type(MediaType.APPLICATION_JSON)  // 指定请求的数据格式为json
                        .post(user);
        }
    复制代码

    复制代码
    @Test
        public void testGet(){
            // 查询一个
            User user =
                WebClient
                        .create("http://localhost:8001/ws/userService/user/1")
                       .accept(MediaType.APPLICATION_JSON)
                        .get(User.class);
            System.out.println(user);
        }
    复制代码

     

    复制代码
    @Test
        public void testUpdate(){
            User user = new User();
            user.setId(100);
            user.setUsername("Jerry");
            user.setCity("gz");
            WebClient
                    .create("http://localhost:8001/ws/userService/user")
                    .accept(MediaType.APPLICATION_XML)
                    .put(user);
            System.out.println(user);
        }
    复制代码

  • 相关阅读:
    linux addr2line 定位so库崩溃位置
    转:关于Android机型适配这件小事儿
    转:android studio 改编译区背景色
    转:ios review推送与执行
    k2pdfopt下载页
    转:让kindle更好的支持pdf
    转:各种文本格式转换的网站
    转: iOS崩溃堆栈符号表使用与用途
    转: 腾讯Bugly干货分享:Android应用性能评测调优
    转: git的图文使用教程(巨详细)
  • 原文地址:https://www.cnblogs.com/dalianpai/p/12349934.html
Copyright © 2011-2022 走看看