cxf整合spring发布rest服务 httpclient访问服务

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>
View Code

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>
View Code

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>
View Code

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 }
View Code

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 }
View Code

实现类

 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 }
View Code

实体类注意添加@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