JAX-RS和spring整合

JAX-RS 和 Spring 整合开发 步骤
  • 1.建立maven web工程
  • 2.建立坐标,在pom.xml中导入需要依赖的jar包
    完整的pom.xml
     <modelVersion>4.0.0</modelVersion>
     <groupId>cn.itcast.maven</groupId>
     <artifactId>cxf_rs_spring</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <packaging>war</packaging>
     <name>cxf_rs_spring</name>
     <dependencies>
          <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>
          <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>org.springframework</groupId>
              <artifactId>spring-context</artifactId>
              <version>4.1.7.RELEASE</version>
          </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-web</artifactId>
              <version>4.1.7.RELEASE</version>
          </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-test</artifactId>
              <version>4.1.7.RELEASE</version>
          </dependency>
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.12</version>
          </dependency>
     </dependencies>
     <build>
          <plugins>
              <plugin>
                   <groupId>org.codehaus.mojo</groupId>
                   <artifactId>tomcat-maven-plugin</artifactId>
                   <version>1.1</version>
                   <configuration>
                        <port>9800</port>
                   </configuration>
              </plugin>
          </plugins>
     </build>
</project>

  • 3.配置web.xml
    完整的web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
     xmlns=" http://java.sun.com/xml/ns/javaee" ;;;
     id="WebApp_ID" version="2.5">
     
     <!-- spring配置文件位置 -->
     <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext.xml</param-value>
     </context-param>
     <!-- spring核心监听器 -->
     <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
     
     <servlet>
          <servlet-name>CXFService</servlet-name>
          <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
          <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
          <servlet-name>CXFService</servlet-name>
          <url-pattern>/ services /*</url-pattern>
     </servlet-mapping>
     
     <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>


  • 4.导入实体类和service
  • 5.、 在 spring 配置发布 rs 服务 
        引入名称空间
 
    配置服务:
完整的applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
     xsi:schemaLocation="
      <jaxrs:server id="xxxService" address="/xxxService" >
          <jaxrs:serviceBeans>
              <bean class="cn.itcast.cxf.service.xxxServiceImpl" />
          </jaxrs:serviceBeans>
          <jaxrs:inInterceptors>
              <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
          </jaxrs:inInterceptors>
          <jaxrs:outInterceptors>
              <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
          </jaxrs:outInterceptors>
     </jaxrs:server>
     
</beans>
    

.配置服务启动端口(如果上面的pom.xml中没有配置,才需要配置这个,上面已经有了完整了pom.xml)
<build>
          <plugins>
              <plugin>
                   <groupId>org.codehaus.mojo</groupId>
                   <artifactId>tomcat-maven-plugin</artifactId>
                   <version>1.1</version>
                   <configuration>
                        <port>9800</port>
                   </configuration>
              </plugin>
          </plugins>
     </build>
  • 6.编写客户端代码 类似独立服务客户端代码 WebClient 工具实现
        1) .在src/test/resource下面创建一个applicationContext-test.xml
        内容如下:
      
    
2).创建client类
package cn.itcast.cxf.client;
import java.util.Collection;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.cxf.jaxrs.client.WebClient;
import cn.itcast.cxf.domain.User;
/**      
* @author: yangzhaoli
* @creationTime:2017年11月13日 下午6:44:34
* @version: 1.1
*/
public class Cxf_rs_springClient {
    public static void main(String[] args) {
        /*//查询根据id
        WebClient client2 = WebClient.create(" http://localhost:9800/cxf_rs_spring/services/userService/user/1" );
        User user2 = client2.accept(MediaType.APPLICATION_JSON). get (User.class);
        System.out.println(user2);*/
        //查询所有
        /*WebClient webClient = WebClient.create(" http://localhost:9800/cxf_rs_spring/services/userService/user").accept(MediaType.APPLICATION_XML );
        Collection<? extends User> collection = webClient. getCollection (User.class);
        System.out.println(collection);*/
        //增加
        /*User user = new User();
        user.setId(9999);
        user.setUsername("haha");
        WebClient webClient = WebClient.create(" http://localhost:9800/cxf_rs_spring/services/userService/user" );
        webClient.type(MediaType.APPLICATION_JSON). post (user);*/
        //修改
        WebClient client = WebClient.create(" http://localhost:9800/cxf_rs_spring/services/userService/user" );
        User user = new User();
        user.setId(2);
        user.setUsername("hhh");
        client. put (user);
    }
}
    注意:最终访问资源服务路径: 服务器根目录地址 + web.xml 配置(url ) + applicationContext.xml address 配置 + 类 @Path + 方法 @Path
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值