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. [React & Debug] Quick way to debug Stateless component

    For example we have the following code: const TodoList = (props) => ( <div className="Tod ...

  2. Git 经常使用命令

    Git经常使用命令备忘: Git配置 git config --global user.name "storm" git config --global user.email &q ...

  3. 主定理(Master Theorem)与时间复杂度

    1. 问题 Karatsuba 大整数的快速乘积算法的运行时间(时间复杂度的递推关系式)为 T(n)=O(n)+4⋅T(n/2),求其最终的时间复杂度. 2. 主定理的内容 3. 分析 所以根据主定理 ...

  4. PythonNET网络编程1

    # PythonNET 网络编程 ISO(国际标准化组织) 制定了 OSI(Open System Interconnectio),意为开放式系统互联.国际标准化组织(ISO)制定了OSI模型,该模型 ...

  5. (转)Windows2008优化IIS7.5支持10万个同时请求的配置方法

    通过对IIS7的配置进行优化,调整IIS7应用池的队列长度,请求数限制,TCPIP连接数等方面,从而使WEB服务器的性能得以提升,保证WEB访问的访问流畅. 在运行中cmd后,输入:C:\Window ...

  6. 代码适配Masonry使用的详细介绍

    Masonry简介 Masonry是一个轻量级的布局框架,它拥有自己的描述语法(采用更优雅的链式语法封装)来自动布局,具有很好可读性且同时支持iOS和Max OS X等.总之,对于侧重写代码的code ...

  7. vue学习笔记二:v-if和v-show的区别

    v-if vs v-show v-if 是“真正的”条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建. v-if 也是惰性的:如果在初始渲染时条件为假,则什么也不做—— ...

  8. 调试 之gdb thread命令 与 ltrace/strace

    我们可以通过  1)  gdb prog_name -> r               用在逐步调试自己的程序时 2)  gdb -> attach process_id       正 ...

  9. php 转义html标签 反转html标签 符号

    $a = '<strong>123</strong>';     //假设这是html代码$b =  htmlspecialchars($a);          //将< ...

  10. Coverage报告生成

    Coverage报告生成 覆盖率 覆盖率驱动的验证方法中覆盖率报告的生成至关重要,现在介绍一下使用DVE和URG生成覆盖率报告的步骤. 使用VCS生成数据 在VCS的运行脚本中添加-cm cond+f ...