用cxf开发restful风格的WebService

2023-07-11,,

我们都知道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风格的WebService.doc》

下载本文的Word格式文档,以方便收藏与打印。