用cxf开发restful风格的WebService
我们都知道cxf还可以开发restful风格的webService,下面是利用maven+spring4+cxf搭建webService服务端和客户端Demo
1、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jacky</groupId>
<artifactId>cxf_restful_server</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>cxf_restful_server Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.json-lib</artifactId>
<version>2.4_1</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency> <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.0.0-milestone1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b01</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<finalName>cxf_restful_server</finalName>
</build>
</project>
2、cxf-restful-server.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:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd"> <!-- 使用注解式注入 -->
<context:annotation-config />
<!-- 自动扫描 -->
<context:component-scan base-package="com.jacky" />
<bean id="rs" class="com.jacky.service.impl.RESTSampleSource"></bean> <jaxrs:server address="/" id="RESTWebService">
<jaxrs:serviceBeans>
<ref bean="rs"/> <!--如果还有其它的服务,继续在这里引用-->
</jaxrs:serviceBeans> <jaxrs:providers>
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
<bean class="org.apache.cxf.jaxrs.provider.JAXBElementProvider" />
</jaxrs:providers>
</jaxrs:server> </beans>
3、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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:oscache="http://www.springmodules.org/schema/oscache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
<import resource="cxf-restful-server.xml"/>
<!-- 使用注解式注入 -->
<context:annotation-config />
<!-- 自动扫描 -->
<context:component-scan base-package="com.jacky" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!--调用restful服务端方式1-->
<bean id="webClient" class="org.apache.cxf.jaxrs.client.WebClient" factory-method="create">
<constructor-arg type="java.lang.String" value="http://localhost:8080/cxf_restful_server/cxf/" />
</bean>
<!--调用restful服务端方式2-->
<bean id="rESTSample" class="org.apache.cxf.jaxrs.client.JAXRSClientFactory" factory-method="create">
<constructor-arg type="java.lang.String" value="http://localhost:8080/cxf_restful_server/cxf/" />
<constructor-arg type="java.lang.Class" value="com.jacky.service.RESTSample" />
</bean> </beans>
4、User.java 实体类
package com.jacky.entity;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable; @XmlRootElement(name = "UserInfo")
public class User implements Serializable {
private static final long serialVersionUID = 677484458789332877L;
private int id;
private String name;
private String email;
private String address; public void setId(int id) {
this.id = id;
} public void setName(String name) {
this.name = name;
} public void setEmail(String email) {
this.email = email;
} public void setAddress(String address) {
this.address = address;
} public static long getSerialVersionUID() {
return serialVersionUID;
} public int getId() {
return id;
} public String getName() {
return name;
} public String getEmail() {
return email;
} public String getAddress() {
return address;
} @Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", email=" + email
+ ", address=" + address + "]";
}
}
5、RESTSample.java 接口
package com.jacky.service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo; import com.jacky.entity.User; import java.io.IOException; @Path(value = "/sample")
public interface RESTSample {
@GET
@Path(value ="/doGet")
@Produces(MediaType.TEXT_PLAIN)
public String doGet(); @GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/request/{param}")
public String doRequest(@PathParam("param") String param,
@Context HttpServletRequest servletRequest,
@Context HttpServletResponse servletResponse) ; @GET
@Path("/bean/{id}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public User getBean(@PathParam("id") int id) ; @GET
@Path("/message/{id}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public String getMessage(@PathParam("id") int id); /*
@Consumes:声明该方法可以接受的MIME
@FormParam:注入该方法的 HTML 属性确定的表单输入。
@Produces :表示该方法返回的MIME类型
*/
@POST
@Path("/postData")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public User postData(User user) throws IOException ; @PUT
@Path("/putData/{id}")
@Produces({MediaType.APPLICATION_XML})
public User putData(@PathParam("id") int id, User user); @DELETE
@Path("/removeData/{id}")
public void deleteData(@PathParam("id") int id) ;
}
6、RESTSampleSource.java 接口的实现
package com.jacky.service.impl; import javax.servlet.http.HttpServletRequest; import net.sf.json.JSONObject; import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo; import com.jacky.entity.User;
import com.jacky.service.RESTSample; import java.io.IOException; /*
注释(Annotation):在 javax.ws.rs.* 中定义,是 JAX-RS (JSR 311) 规范的一部分。
@Path:定义资源基 URI。由上下文根和主机名组成,资源标识符类似于 http://localhost:8080/RESTful/rest/hello。
@GET:这意味着以下方法可以响应 HTTP GET 方法。
@Produces:以纯文本方式定义响应内容 MIME 类型。 @Context: 使用该注释注入上下文对象,比如 Request、Response、UriInfo、ServletContext 等。
@Path("{contact}"):这是 @Path 注释,与根路径 “/contacts” 结合形成子资源的 URI。
@PathParam("contact"):该注释将参数注入方法参数的路径,在本例中就是联系人 id。其他可用的注释有 @FormParam、@QueryParam 等。
@Produces:响应支持多个 MIME 类型。在本例和上一个示例中,APPLICATION/XML 将是默认的 MIME 类型。
*/ @Path(value = "/sample")
public class RESTSampleSource implements RESTSample { @Context
private UriInfo uriInfo; @Context
private Request request; @GET
@Path(value ="/doGet")
@Produces(MediaType.TEXT_PLAIN)
public String doGet() {
System.out.println("测试时doGet");
return "this is get rest request";
} @GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/request/{param}")
public String doRequest(@PathParam("param") String param,
@Context HttpServletRequest servletRequest, @Context HttpServletResponse servletResponse) {
System.out.println(servletRequest);
System.out.println(servletResponse);
System.out.println("param======"+param);
System.out.println(servletRequest.getContentType());
System.out.println(servletResponse.getCharacterEncoding());
try {
System.out.println(servletResponse.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
return "success";
} @GET
@Path("/bean/{id}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public User getBean(@PathParam("id") int id) {
System.out.println("============getBean==============");
System.out.println("id:" + id);
System.out.println("客户端请求方式:" + request.getMethod());
System.out.println("客户端请求的uri:" + uriInfo.getPath());
System.out.println(uriInfo.getPathParameters()); User user = new User();
user.setId(id);
user.setName("lulu");
return user;
} @GET
@Path("/message/{id}")
@Produces(MediaType.APPLICATION_JSON)
public String getMessage(@PathParam("id") int id) {
JSONObject jsonObject=new JSONObject();
User user = new User();
user.setId(id);
user.setName("JojO");
return jsonObject.fromObject(user).toString();
} @POST
@Path("/postData")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public User postData(User user) throws IOException {
System.out.println("user=========="+user);
user.setName("jacky");
return user;
} @PUT
@Path("/putData/{id}")
@Produces({ MediaType.APPLICATION_XML })
public User putData(@PathParam("id") int id, User user) {
System.out.println("=========putData========");
System.out.println(user);
user.setId(id);
user.setAddress("hoojo#gz");
user.setEmail("hoojo_@126.com");
user.setName("hoojo");
System.out.println(user);
return user;
} @DELETE
@Path("/removeData/{id}")
public void deleteData(@PathParam("id") int id) {
System.out.println("======deleteData=========" + id);
}
}
7、Client.java
package com.jacky.client; import org.apache.cxf.jaxrs.client.WebClient;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jacky.service.RESTSample; public class Client {
@Autowired
private RESTSample rESTSample;
@Autowired
private WebClient webClient;
@Before
public void initContext(){
ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
webClient=(WebClient)ac.getBean("webClient");
rESTSample=(RESTSample)ac.getBean("rESTSample");
}
@Test
public void testWebService(){
/*第一种调用接口方式
优点:不需要服务端提供接口
缺点:不够方便*/
String user= webClient.path("sample/message/6").accept("MediaType.APPLICATION_JSON").get(String.class);
System.out.println(user);
} @Test
public void testWebService2(){
/*第二种调用接口方式
优点:调用方便
缺点:需要服务端提供接口*/
String user= rESTSample.getMessage(6);
System.out.println(user);
}
}
到这里用cxf开发restful就完成了
欢迎关注
用cxf开发restful风格的WebService的更多相关文章
- 基于cxf开发restful风格的Web Service
一.写在前面 webservice一些简单的其他用法和概念,就不在这里赘述了,相信大家都可以在网上查到,我也是一个新手,写这篇文章的目的一方面是想记录自己成长的历程,另一方面是因为学习这个的时候花了点 ...
- IDEA java开发 Restful 风格的WebService
官网:https://www.jetbrains.com/help/idea/restful-webservices.html 1.在IntelliJ中创建新项目,选择Java Enterprise ...
- 使用CXF开发RESTFul服务
相信大家在阅读CXF官方文档(http://cxf.apache.org/docs/index.html)时,总是一知半解.这里向大家推荐一本PacktPub.Apache.CXF.Web.Servi ...
- SpringMVC实现Restful风格的WebService
1.环境 JDK7 MyEclipse2014 tomcat8 maven 3.3.3 spring4.1.4 2.创建maven工程 使用MyEclipse创建maven工程的方式可以参考这篇博文( ...
- 使用webpy创建一个简单的restful风格的webservice应用
下载:wget http://webpy.org/static/web.py-0.38.tar.gz解压并进入web.py-0.38文件夹安装:easy_install web.py 这是一个如何使用 ...
- 使用Spring boot开发RestFul 风格项目PUT/DELETE方法不起作用
在使用Spring boot 开发restful 风格的项目,put.delete方法不起作用,解决办法. 实体类Student @Data public class Student { privat ...
- restful风格的webservice开发之概念准备篇
理解restful:什么是rest,rest是Representational State Transfer的缩写,翻译过来就是“表现层状态转化”.这里有点不准确,其实rest省略了主语表现层指的是资 ...
- cxf开发Restful Web Services
一.restful web services rest全称是Representation State Transfer(表述性状态转移).它是一种软件架构风格,只是提供了一组设计原则和约束条件.在re ...
- 使用CXF开发JAX-WS类型的WebService
使用CXF记得要先加入CXF的jar包 方法1: Cxf编程实现: l 使用jaxwsServerFactoryBean发布 webservice服务端. 需要设置: jaxwsServerFacto ...
随机推荐
- NPOI操作excel之读取excel数据
NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 一.下载引用 去NPOI官网http://npoi.codeplex. ...
- fbset 移植
手头上的文件系统的fbset有问题,所以就自己从新移植一个到开发板上. 参考链接 http://blog.chinaunix.net/uid-20768928-id-5748009.html 下载地址 ...
- Python开发【第三章】:Python函数介绍
一. 函数介绍 1.函数是什么? 在学习函数之前,一直遵循面向过程编程,即根据业务逻辑从上到下实现功能,其往往用一长段代码来实现指定功能,开发过程中最常见的操作就是粘贴复制,也就是将之前实现的代码块复 ...
- 使用Karma 来进行 JavaScript 测试
最近接触了一些新的前端开发知识,主要是利用AngularJS做single page application.我也借这个机会,花了几天时间了解了如何对javascript进行测试. 这里将介绍一些使用 ...
- 帝国CMS列表模板页面内容截取
$listtemp = '<div class="c_n_item">';$listtemp .= '<div class="c_n_title&quo ...
- Tuple,Array,Map,文件操作
Tuple是一个元组: 1,)一组元组中支持多个元素: 2,)一组元组中可以包含不同类型的元素: 3,)返回下标从_1开始. Array 数组 var items=Array(1,2,3,4) Map ...
- 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数002·AI人工智能
<zw版·Halcon-delphi系列原创教程> Halcon分类函数002·AI人工智能 AI人工智能:包括knn.gmm.svm等 为方便阅读,在不影响说明的前提下,笔者对函数进行了 ...
- Android实现归属地查询功能
实现归属地查询大体有两种方法可以实现,一种是通过归属地API进行查询,另一种是查询本地数据库.两种方法各有优劣,最好结合起来使用,我本次采用的是查询数据库的方法.首先需要从网上下载归属地数据库call ...
- xcode 中 的工程模板
基于视图的应用程序(view-based application) 应用程序如果仅使用一个视图,应该使用这个模板.一个简单的视图控制器会管理应用程序的主视图,而界面布置则使用一个Interface B ...
- (转) cocos2dx 显示中文
cocos2dx 中文显示 分类: cocos2dx 2014-01-18 10:31 253人阅读 评论(0) 收藏 举报 目录(?)[+] 总结一句话,就是UTF-8编码. 1. 中文FNT字体文 ...