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

pom.xml

 <properties>
<webVersion>3.0</webVersion>
<cxf.version>3.2.5</cxf.version>
<spring.version>4.3.18.RELEASE</spring.version>
<jettison.version>1.4.0</jettison.version>
</properties> <dependencies>
<!--spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency> <!-- cxf依赖 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>${cxf.version}</version>
</dependency>
<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-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency> <!-- rest风格支持 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- json支持 -->
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>${jettison.version}</version>
</dependency>
</dependencies>

2.配置spring的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 address="/studentquery">
<jaxrs:serviceBeans>
<ref bean="studentInterface"/>
</jaxrs:serviceBeans>
</jaxrs:server> <!-- 服务实现类 -->
<bean id="studentInterface" class="cxf_rest_server.student.StudentInterfaceImpl"/>
</beans>

3.配置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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>cxf_rest_spring_server</display-name>
<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> <!-- 配置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> <!-- 配置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>/ss/*</url-pattern>
</servlet-mapping> </web-app>

4.代码

实体类

 package cxf_rest_server.student;

 import javax.xml.bind.annotation.XmlRootElement;

 @XmlRootElement(name="student")
public class Student {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

SEI接口

 package cxf_rest_server.student;

 import java.util.List;

 import javax.jws.WebService;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; @WebService
@Path("/student")
public interface StudentInterface {
@POST
@Path("/query/{id}")
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public Student query(@PathParam("id")int id); @GET
@Path("/queryList/{id}/{name}")
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public List<Student> queryList(@PathParam("id")int id,@PathParam("name")String name);
}

实现类

 package cxf_rest_server.student;

 import java.util.ArrayList;
import java.util.List; public class StudentInterfaceImpl implements StudentInterface { @Override
public Student query(int id) {
Student student = new Student();
student.setId(1);
student.setName("小明");
return student;
} @Override
public List<Student> queryList(int id,String name) {
Student student1 = new Student();
student1.setId(1);
student1.setName("小明"); Student student2 = new Student();
student2.setId(2);
student2.setName("小李"); Student student3 = new Student();
student3.setId(3);
student3.setName("小华");
List<Student> list = new ArrayList<Student>();
list.add(student1);
list.add(student2);
list.add(student3);
return list;
} }

实体类注意添加@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访问服务的简单示例

添加以下依赖

 <properties>
<httpClient.version>4.5.6</httpClient.version>
<junit.version>4.9</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpClient.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency> </dependencies>
 package cxf_rest_server.student;

 import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test; /**
* 使用httpClient访问服务
* @author tele
*
*/
public class StudentClient {
@Test
public void get() throws Exception{
//创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建get对象
HttpGet get = new HttpGet("http://127.0.0.1:8080/cxf_rest_spring_server/ss/studentquery/student/queryList/100/123");
//执行请求
CloseableHttpResponse response = httpClient.execute(get);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode == 200){
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity,"utf-8");
System.out.println(result);
}
response.close();
httpClient.close();
} @Test
public void post() throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost("http://127.0.0.1:8080/cxf_rest_spring_server/ss/studentquery/student/query/100");
CloseableHttpResponse response = httpClient.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode == 200) {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity,"utf-8");
System.out.println(result);
}
response.close();
httpClient.close();
}
}

7.数据的解析

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

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

cxf整合spring发布rest服务 httpclient访问服务的更多相关文章

  1. CXF整合Spring发布WebService实例

    一.说明: 上一篇简单介绍了CXF以及如何使用CXF来发布一个简单的WebService服务,并且介绍了客户端的调用. 这一篇介绍如何使用CXF与spring在Web项目中来发布WebService服 ...

  2. webservice 服务端例子+客户端例子+CXF整合spring服务端测试+生成wsdl文件 +cxf客户端代码自动生成

    首先到CXF官网及spring官网下载相关jar架包,这个不多说.webservice是干嘛用的也不多说. 入门例子 模拟新增一个用户,并返回新增结果,成功还是失败. 大概的目录如上,很简单. Res ...

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

    一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId> ...

  4. 【Java EE 学习 81】【CXF框架】【CXF整合Spring】

    一.CXF简介 CXF是Apache公司下的项目,CXF=Celtix+Xfire:它支持soap1.1.soap1.2,而且能够和spring进行快速无缝整合. 另外jax-ws是Sun公司发布的一 ...

  5. CXF整合Spring开发WebService

    刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开 ...

  6. 【WebService】——CXF整合Spring

    相关博客: [WebService]--入门实例 [WebService]--SOAP.WSDL和UDDI 前言: 之前的几篇博客基本上都是使用jdk来实现WebService的调用,没有使用任何框架 ...

  7. CXF整合Spring之JaxWsProxyFactoryBean调用

    1.见解 1.1 客户端的接口代码还一定要和服务端的接口代码一样,连注解都要一样,不够灵活 1.2 当客户端访问服务器的请求地址时,如果服务端没有对应的地址,就会报错,但是又没有cxf的异常捕获处理 ...

  8. cxf整合spring错误为:cvc-complex-type.2.4.c

    cxf整合spring,报错信息如下: Multiple annotations found at this line:- cvc-complex-type.2.4.c: The matching w ...

  9. cxf整合spring中出现的错误

    Caused by: java.lang.ClassNotFoundException: javax.wsdl.extensions.ElementExtensible at org.apache.c ...

随机推荐

  1. Costura.Fody

    使用Costura.Fody将源DLL合并到目标EXE 本文为原创文章,如转载,请在网页明显位置标明原文名称.作者及网址,谢谢! 一.本文主要是使用Costura.Fody工具将源DLL合并到目标EX ...

  2. html 代码

    1.结构性定义 文件类型 <HTML></HTML> (放在档案的开头与结尾) 文件主题 <TITLE></TITLE> (必须放在「文头」区块内) 文 ...

  3. 关于JS的面向对象总结

    什么是面向对象: 对象由两部分构成:属性 和 方法: 面向对象的特点: 1.封装:对于相同功能的代码,放在一个函数中,以后再用到此功能,只需要调用即可,无需再重写:避免大量冗余代码: 专业话说:低耦合 ...

  4. 安装配置Rancher管理docker

    原文:安装配置Rancher管理docker 版权声明:本文为博主原创文章,转载请注明地址http://blog.csdn.net/tianyaleixiaowu. https://blog.csdn ...

  5. 模拟登录QQ推断是否须要验证码

    老生常谈的问题了,在模拟登录之前,推断是否须要验证码: https://ssl.ptlogin2.qq.com/check? uin=QQ号码&appid=1003903&js_ver ...

  6. CloudFoundry hm9000原理及排错

    hm9000跟hm_next(healthmanager)功能类似.在cloudfoundry集群中担任至关重要的角色 - 尝试启动缺失情况下的实例,停止异常实例 - 获知和报告应用执行的实际实例个数 ...

  7. POJ 1679 The Unique 次最小生成树 MST

    http://poj.org/problem?id=1679 题目大意: 给你一些点,判断MST(最小生成树)是否唯一. 思路: 以前做过这题,不过写的是O(n^3)的,今天学了一招O(n^2)的,哈 ...

  8. 【Codeforces Round #435 (Div. 2) B】Mahmoud and Ehab and the bipartiteness

    [链接]h在这里写链接 [题意] 让你在一棵树上,加入尽可能多的边. 使得这棵树依然是一张二分图. [题解] 让每个节点的度数,都变成二分图的对方集合中的点的个数就好. [错的次数] 0 [反思] 在 ...

  9. ZOJ 1108 FatMouse's Speed (HDU 1160) DP

    传送门: ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=108 HDU :http://acm.hdu.edu.cn/s ...

  10. 计算机图形学(二)输出图元_3_画线算法_2_DDA算法

    DDA算法        数字微分分析仪(digital differential analyzer, DDA)方法是一种线段扫描转换算法.基于使用等式(3.4)或等式(3.5)计算的&x或& ...