cxf整合spring发布rest服务

1.创建maven web项目并添加依赖

pom.xml

 1 <properties>
 2       <webVersion>3.0</webVersion>
 3       <cxf.version>3.2.5</cxf.version>
 4       <spring.version>4.3.18.RELEASE</spring.version>
 5       <jettison.version>1.4.0</jettison.version>
 6   </properties>
 7
 8   <dependencies>
 9         <!--spring依赖-->
10         <dependency>
11             <groupId>org.springframework</groupId>
12             <artifactId>spring-core</artifactId>
13             <version>${spring.version}</version>
14         </dependency>
15           <dependency>
16                 <groupId>org.springframework</groupId>
17                 <artifactId>spring-web</artifactId>
18                 <version>${spring.version}</version>
19         </dependency>
20          <dependency>
21             <groupId>org.springframework</groupId>
22             <artifactId>spring-beans</artifactId>
23             <version>${spring.version}</version>
24         </dependency>
25         <dependency>
26             <groupId>org.springframework</groupId>
27             <artifactId>spring-context-support</artifactId>
28             <version>${spring.version}</version>
29         </dependency>
30         <dependency>
31             <groupId>org.springframework</groupId>
32             <artifactId>spring-context</artifactId>
33             <version>${spring.version}</version>
34         </dependency>
35      <!-- <dependency>
36             <groupId>org.springframework</groupId>
37             <artifactId>spring-aop</artifactId>
38             <version>${spring.version}</version>
39         </dependency>
40         -->
41         <dependency>
42             <groupId>org.slf4j</groupId>
43             <artifactId>slf4j-simple</artifactId>
44             <version>1.7.25</version>
45         </dependency>
46
47
48         <!-- cxf依赖 -->
49           <dependency>
50             <groupId>org.apache.cxf</groupId>
51             <artifactId>cxf-core</artifactId>
52             <version>${cxf.version}</version>
53         </dependency>
54           <dependency>
55             <groupId>org.apache.cxf</groupId>
56             <artifactId>cxf-rt-frontend-jaxws</artifactId>
57             <version>${cxf.version}</version>
58         </dependency>
59         <dependency>
60             <groupId>org.apache.cxf</groupId>
61             <artifactId>cxf-rt-transports-http</artifactId>
62             <version>${cxf.version}</version>
63         </dependency>
64         <dependency>
65             <groupId>org.apache.cxf</groupId>
66             <artifactId>cxf-rt-transports-http-jetty</artifactId>
67             <version>${cxf.version}</version>
68         </dependency>
69
70         <!-- rest风格支持 -->
71         <dependency>
72             <groupId>org.apache.cxf</groupId>
73             <artifactId>cxf-rt-frontend-jaxrs</artifactId>
74             <version>${cxf.version}</version>
75         </dependency>
76         <dependency>
77             <groupId>org.apache.cxf</groupId>
78             <artifactId>cxf-rt-rs-extension-providers</artifactId>
79             <version>${cxf.version}</version>
80         </dependency>
81         <!-- json支持 -->
82         <dependency>
83             <groupId>org.codehaus.jettison</groupId>
84             <artifactId>jettison</artifactId>
85             <version>${jettison.version}</version>
86         </dependency>
87   </dependencies>

2.配置spring的applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
 4     xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6                         http://www.springframework.org/schema/beans/spring-beans.xsd
 7                         http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
 8                         http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
 9                         http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
10
11     <jaxrs:server address="/studentquery">
12         <jaxrs:serviceBeans>
13             <ref bean="studentInterface"/>
14         </jaxrs:serviceBeans>
15     </jaxrs:server>
16
17     <!-- 服务实现类 -->
18     <bean id="studentInterface" class="cxf_rest_server.student.StudentInterfaceImpl"/>
19 </beans>

3.配置web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>cxf_rest_spring_server</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12
13
14   <!-- 配置spring文件环境 -->
15   <context-param>
16       <param-name>contextConfigLocation</param-name>
17     <param-value>classpath:applicationContext.xml</param-value>
18   </context-param>
19
20   <!-- 监听器 -->
21   <listener>
22       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
23   </listener>
24
25
26   <!-- 配置CXF的Servlet -->
27   <servlet>
28       <servlet-name>CXF</servlet-name>
29       <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
30   </servlet>
31   <servlet-mapping>
32       <servlet-name>CXF</servlet-name>
33       <url-pattern>/ss/*</url-pattern>
34   </servlet-mapping>
35
36 </web-app>

4.代码

实体类

 1 package cxf_rest_server.student;
 2
 3 import javax.xml.bind.annotation.XmlRootElement;
 4
 5 @XmlRootElement(name="student")
 6 public class Student {
 7     private int id;
 8     private String name;
 9     public int getId() {
10         return id;
11     }
12     public void setId(int id) {
13         this.id = id;
14     }
15     public String getName() {
16         return name;
17     }
18     public void setName(String name) {
19         this.name = name;
20     }
21 }

SEI接口

 1 package cxf_rest_server.student;
 2
 3 import java.util.List;
 4
 5 import javax.jws.WebService;
 6 import javax.ws.rs.GET;
 7 import javax.ws.rs.POST;
 8 import javax.ws.rs.Path;
 9 import javax.ws.rs.PathParam;
10 import javax.ws.rs.Produces;
11 import javax.ws.rs.core.MediaType;
12
13 @WebService
14 @Path("/student")
15 public interface StudentInterface {
16     @POST
17     @Path("/query/{id}")
18     @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
19     public Student query(@PathParam("id")int id);
20
21
22     @GET
23     @Path("/queryList/{id}/{name}")
24     @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
25     public List<Student> queryList(@PathParam("id")int id,@PathParam("name")String name);
26 }

实现类

 1 package cxf_rest_server.student;
 2
 3 import java.util.ArrayList;
 4 import java.util.List;
 5
 6 public class StudentInterfaceImpl implements StudentInterface {
 7
 8     @Override
 9     public Student query(int id) {
10         Student student = new Student();
11         student.setId(1);
12         student.setName("小明");
13         return student;
14     }
15
16     @Override
17     public List<Student> queryList(int id,String name) {
18         Student student1 = new Student();
19         student1.setId(1);
20         student1.setName("小明");
21
22         Student student2 = new Student();
23         student2.setId(2);
24         student2.setName("小李");
25
26         Student student3 = new Student();
27         student3.setId(3);
28         student3.setName("小华");
29         List<Student> list = new ArrayList<Student>();
30         list.add(student1);
31         list.add(student2);
32         list.add(student3);
33         return list;
34     }
35
36 }

实体类注意添加@XmlRootElement注解

接口添加jaxrs注解,定义数据格式

5.部署至tomcat后打开浏览器采用以下格式访问

url-pattern:web.xml中定义的cxf的servlet拦截目录

address:applicationContext.xml中的<jaxrs:server>标签定义的address

path:在接口中使用@Path注解设定的值

http://ip:port/项目名/url-pattern/address/path/方法路径/参数

6.由于使用浏览器url的方式只能访问注解为@GET的方法,下面给出使用HttpClient访问服务的简单示例

添加以下依赖

 1 <properties>
 2       <httpClient.version>4.5.6</httpClient.version>
 3       <junit.version>4.9</junit.version>
 4   </properties>
 5   <dependencies>
 6     <dependency>
 7         <groupId>org.apache.httpcomponents</groupId>
 8         <artifactId>httpclient</artifactId>
 9         <version>${httpClient.version}</version>
10     </dependency>
11     <dependency>
12         <groupId>junit</groupId>
13         <artifactId>junit</artifactId>
14         <version>${junit.version}</version>
15         <scope>test</scope>
16     </dependency>
17
18   </dependencies>
 1 package cxf_rest_server.student;
 2
 3 import org.apache.http.HttpEntity;
 4 import org.apache.http.client.methods.CloseableHttpResponse;
 5 import org.apache.http.client.methods.HttpGet;
 6 import org.apache.http.client.methods.HttpPost;
 7 import org.apache.http.impl.client.CloseableHttpClient;
 8 import org.apache.http.impl.client.HttpClients;
 9 import org.apache.http.util.EntityUtils;
10 import org.junit.Test;
11
12 /**
13  * 使用httpClient访问服务
14  * @author tele
15  *
16  */
17 public class StudentClient {
18     @Test
19     public void get() throws Exception{
20         //创建httpclient对象
21         CloseableHttpClient httpClient = HttpClients.createDefault();
22         //创建get对象
23         HttpGet get = new HttpGet("http://127.0.0.1:8080/cxf_rest_spring_server/ss/studentquery/student/queryList/100/123");
24         //执行请求
25         CloseableHttpResponse response = httpClient.execute(get);
26         int statusCode = response.getStatusLine().getStatusCode();
27         if(statusCode == 200){
28             HttpEntity entity = response.getEntity();
29             String result = EntityUtils.toString(entity,"utf-8");
30             System.out.println(result);
31         }
32         response.close();
33         httpClient.close();
34     }
35
36     @Test
37     public void post() throws Exception {
38         CloseableHttpClient httpClient = HttpClients.createDefault();
39         HttpPost post = new HttpPost("http://127.0.0.1:8080/cxf_rest_spring_server/ss/studentquery/student/query/100");
40         CloseableHttpResponse response = httpClient.execute(post);
41         int statusCode = response.getStatusLine().getStatusCode();
42         if(statusCode == 200) {
43             HttpEntity entity = response.getEntity();
44             String result = EntityUtils.toString(entity,"utf-8");
45             System.out.println(result);
46         }
47         response.close();
48         httpClient.close();
49     }
50 }

7.数据的解析

拿到数据后,根据数据格式进行解析,如果是xml,可以使用jdom,dom4j

如果是json........

原文地址:https://www.cnblogs.com/tele-share/p/9374023.html

时间: 2024-11-07 15:03:05

cxf整合spring发布rest服务的相关文章

CXF结合spring发布WS服务,含SOAP services、RESTful services

1.访问:http://localhost:8088/sniperWS/services/查看有哪些服务,包含Available SOAP services.Available RESTful services 2.客户端调用RESTful services:http://localhost:8088/sniperWS/services/address/getSuggestions.query 调用示例:$.ajax({    url: "http://ip:port/sniperWS/serv

WebService—CXF整合Spring实现接口发布和调用过程2

一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.18</version> </dependency> <de

WebService—CXF整合Spring实现接口发布和调用过程

一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.18</version> </dependency> <de

【Java EE 学习第81天】【CXF框架】【CXF整合Spring】

一.CXF简介 CXF是Apache公司下的项目,CXF=Celtix+Xfire:它支持soap1.1.soap1.2,而且能够和spring进行快速无缝整合. 另外jax-ws是Sun公司发布的一套开发WebService服务的标准.早期的标准如jax-rpc已经很少使用,而cxf就是在新标准jax-ws下开发出来的WebService,jax-ws也内置到了jdk1.6当中. CXF官方下载地址:http://cxf.apache.org/download.html 下载完成之后,解压开压

CXF整合Spring开发WebService

刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开环境变量配置窗口,点击新建,新建%CXF_HOME%变量,值为你下载的cxf所在的目录,我的是D:\tools\apache-cxf-3.1.0 2.在Path变量中新加一句%CXF_HOME%\lib,注意要和已有的path变量用;隔开 3.在CLASSPATH中新加一句%CXF_HOME%\li

使用CXF框架,发布webservice服务,并使用客户端远程访问webservice

使用CXF框架,发布webservice服务,并使用客户端远程访问webservice  1. CXF介绍 :soa的框架    * cxf 是 Celtrix (ESB框架)和 XFire(webserivice) 合并而成,并且捐给了apache      * CxF的核心是org.apache.cxf.Bus(总线),类似于Spring的 ApplicationContext    * CXF默认是依赖于Spring的    * Apache CXF 发行包中的jar,如果全部放到lib中

(五) 整合spring cloud云服务架构 - 云架构代码结构构建

上一篇介绍了<整合spring cloud云服务架构 - 企业分布式微服务云架构图>,本篇我们根据架构图进行代码的构建.根据微服务化设计思想,结合spring cloud一些优秀的项目,如服务发现.治理.配置化管理.路由负载.安全控制等优秀解决方案,使用Maven技术将框架进行模块化.服务化.原子化封装并构建,也为后期的灰度发布.持续集成提前做好准备工作. 另外在搭建环境之前,大家需要熟练掌握maven的使用及相关问题的处理(这里不再重复介绍). Spring Cloud云架构使用maven来

Apache CXF 整合Spring

一.创建一个 Java Web 工程,目录最终的结构如下图,下面我们将遂一说明: 二.把我们要用到的jar包全部放到lib目录下. 三.修改web.xml文件,整合CXF. <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2e

(四)整合spring cloud云服务架构 - particle-common-framework代码介绍

上一篇我们介绍了spring cloud云服务架构 - particle云架构代码结构,简单的按照几个大的部分去构建代码模块,让我们来回顾一下: 第一部分: 针对于普通服务的基础框架封装(entity.dao.service.controller.api)等 第二部分: spring cloud通用微服务项目,可以监控左右微服务,当然,本身自己也是微服务. 第三部分: 针对于框架内所有组件的封装,可以植入任何的模块项目中. 第四部分: 自身项目的微服务业务,比如:会员模块.消息模块.资金模块.订