1.CXF简单介绍:
开源的webservice
框架,
支持SOAP1.1/1.2
,REST
支持XML
,
JSON
CXF是apache旗下的开源框架,由Celtix + XFire这两门经典的框架合成,是一套非常流行的web service框架。
它提供了JAX-WS的全面支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用,同时它能与spring进行完美结合
2.cxf-webService
A.导入依赖pom.xml 配置
<properties> <cxf.version>2.2.7</cxf.version> </properties>
<dependencies>
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency>
<dependencies>
<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>
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-security</artifactId> <version>${cxf.version}</version> </dependency>
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-policy</artifactId> <version>${cxf.version}</version> </dependency>
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-bundle-jaxrs</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency>
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.5.8</version> </dependency>
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.5.8</version>
</dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.0</version> </dependency>
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency>
</dependencies>
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency>
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-security</artifactId> <version>${cxf.version}</version> </dependency>
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-policy</artifactId> <version>${cxf.version}</version> </dependency>
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-bundle-jaxrs</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency>
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.5.8</version> </dependency>
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.5.8</version>
</dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.0</version> </dependency>
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency>
</dependencies>
B.
WEB-INF目录下,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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID" version="2.5">
- <!--spring需要加载的配置文件-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:applicationContext-server.xml
- </param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!--
- cxf服务启动servlet
- -->
- <servlet>
- <servlet-name>CXFServlet</servlet-name>
- <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>CXFServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
C.spring集成CXF配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
</beans>
D.编写接口,将其声明为webservice服务类
因为SOAPBinding.Style不同,在生成webwervice时的一些差异。
SOAPBinding.Style:
PRC、
DOCUMENT
JDK6 中默认的是DOCUMENT JDK1.7使用
PRC
接口上添加@WebService 注解将此类声明为服务类,
@WebParam(name=名称)传入参数 定义服务类参数名称
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.RPC)
public interface IUserService {
public String getUserByName(@WebParam(name = "username") String username);
public void setUser(String username);
}
编写接口实现类
public class UserService implements IUserService {
public String getUserByName(String username) {
return "Hello:"+username;
}
public void setUser(String username) {
System.out.println(username);
}
}
E.配置spring映射文件
jaxws配置中serviceClass 表示配置好的服务类(接口上有webservice注解的类)
address表示访问路径能
serviceBean表示接口的实现类
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="userServiceBean" class="com.UserService" />
<jaxws:server id="userService" serviceClass="com.IUserService"
address="/userService">
<jaxws:serviceBean>
<ref bean="userServiceBean" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
F.启动服务器进行发布部署,在浏览器中输入http://localhost:8080/SpringCXF/services/userService?wsdl 看页面是否会出现xml文档,来判断服务是否配置成功
G:客户端配置
server.client 的配置
<?
xml
version
="1.0"
encoding
="UTF-8"?
>
<
beans
xmlns
="http://www.springframework.org/schema/beans"
xmlns:context
="http://www.springframework.org/schema/context"
xmlns:jaxws
="http://cxf.apache.org/jaxws"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
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/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"
<
import
resource
="classpath:META-INF/cxf/cxf.xml"/>
<
import
resource
="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<
import
resource
="classpath:META-INF/cxf/cxf-servlet.xml"/>
<
jaxws:client
id
="userWsClient"
serviceClass
="com.hoo.service.IComplexUserService"
address
="http://localhost:8080/
/SpringCXF/services/userService
"/>
</
beans
>
2.
RESTful Webservice
RESTful风格的
WebService
主张重用
HTTP
协议,面向资源编程(
ROA
)。扼要的说,
RESTful
风格
WebService
中,每一个
URL
即表示一个资源.
RESTful 简化了 web service 的设计,它不再需要 wsdl ,也不再需要 soap 协议,而是通过最简单的 http 协议传输数据 ( 包括 xml 或 json) 。既简化了设计,也减少了网络传输量(因为只传输代表数据的 xml 或 json ,没有额外的 xml 包装)
A.pom.xml配置,引入依赖的jar包
<dependencies>
<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.4.4</version> </dependency>
</dependencies>
web.xml配置
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<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.4.4</version> </dependency>
</dependencies>
web.xml配置
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
B.restful webService 注解讲解
标签 | 用法和解释 |
@GET,@POST,@PUT,@DELETE | 该组标签将一个标注方法的 HTTP 请求类型 |
@PATH | 该标签可用于类或者方法,表示定位此 RESTful 资源的路径。 |
@Produces | 该标签标注资源类或方法返回的 MIME 类型 , 也就是资源方法产生并且返回给客户端的响应消息的类型。例如 application/xml. |
@Consumes | 该标签标注资源类或方法可以接受的请求消息的类型,也就是客户端发送的请求中可以包含的 Http Entity 的类型。 |
@PathParam,@QueryParam,@HeaderParam,@CookieParam | 该组分别标注方法的参数来自于 HTTP 请求的不同位置,例如,@PathParam 来自于 URL 的路径,@QueryParam 来自于 URL 的查询参数,@HeaderParam 来自于 HTTP 请求的头信息,@CookieParam 来自于 HTTP 请求的 Cookie。 |
@Context | 该标签标注资源请求或者响应的上下文实例,例如我们可以再资源方法参数中注入 HttpServletRequest (@Context HttpServletRequest httpRequest) 来获取 Http 请求的相关信息 |
Demo:
@GET @PATH(value=”/staff/query/{id}”) @Produces(“application/xml”) Staff getStaffById(@PathParam("id") int id)
C.restful webservice注解释义
@XmlAccessorType(XmlAccessType.
PROPERTY
) @XmlRootElement(name = "User") @XmlType(propOrder = { "userId", "nickname", "gender","registerDate"})
publicclassUser
implementsSerializable {
privatestaticfinallong serialVersionUID= 1L;
privateString userId;
privateString nickname;
privateString gender;
privateDate registerDate;
publicUser(String userId, String nickname, String gender, Date registerDate)
{ this.userId = userId; this.nickname = nickname; this.gender = gender; this.registerDate = registerDate; }
publicString getUserId()
{ returnuserId; }
publicvoidsetUserId(String userId)
{ this.userId = userId; }
publicString getNickname()
{ returnnickname; }
publicvoidsetNickname(String nickname)
{ this.nickname = nickname; }
publicString getGender()
{ returngender; }
publicvoidsetGender(String gender)
{ this.gender = gender; }
@XmlJavaTypeAdapter(DateConverter. class )
publicDate getRegisterDate() { returnregisterDate; }
publicvoidsetRegisterDate(Date registerDate)
{ this.registerDate = registerDate; }
@Override publicString toString()
{ return"User [userId=" + userId + ", nickname=" + nickname + ", gender=" + gender + ", registerDate =" + registerDate + "]"; } }
privatestaticfinallong serialVersionUID= 1L;
privateString userId;
privateString nickname;
privateString gender;
privateDate registerDate;
publicUser(String userId, String nickname, String gender, Date registerDate)
{ this.userId = userId; this.nickname = nickname; this.gender = gender; this.registerDate = registerDate; }
publicString getUserId()
{ returnuserId; }
publicvoidsetUserId(String userId)
{ this.userId = userId; }
publicString getNickname()
{ returnnickname; }
publicvoidsetNickname(String nickname)
{ this.nickname = nickname; }
publicString getGender()
{ returngender; }
publicvoidsetGender(String gender)
{ this.gender = gender; }
@XmlJavaTypeAdapter(DateConverter. class )
publicDate getRegisterDate() { returnregisterDate; }
publicvoidsetRegisterDate(Date registerDate)
{ this.registerDate = registerDate; }
@Override publicString toString()
{ return"User [userId=" + userId + ", nickname=" + nickname + ", gender=" + gender + ", registerDate =" + registerDate + "]"; } }
为了用 XML 映射多个用户,我们定义了 Users 类,Users 类只有一个成员变量,它是一个装有 User 对象的 java.util.List 列表。
@XmlRootElement(name = "Users"
)
publicclassUsers
{ @XmlElement(name = "User")
privateList<User> users;
publicList<User> getUsers()
{ returnusers; }
publicvoidsetUsers(List<User> users)
{ this.users = users; } }
publicclassUsers
{ @XmlElement(name = "User")
privateList<User> users;
publicList<User> getUsers()
{ returnusers; }
publicvoidsetUsers(List<User> users)
{ this.users = users; } }
对于注册日期 registerDate 变量的映射,需要做一些必要的处理,我们希望日期被映射为 yyyy-mm-dd 的形 式,上述 User 类中, 我们用 @XmlJavaTypeAdapter 标签 注释 registerDate 的 getter 方法, 希望 告知 JAXB 遇到这个属性的时候, 用 DateConverter 类做处理
@Produces({ MediaType.
APPLICATION_XML
})
@Consumes({ MediaType. APPLICATION_XML })
publicinterface UserService
{ @GET @Produces(MediaType. APPLICATION_XML ) @Path("/searchuser/{userId}")
User searchUser( @PathParam("userId") String userId);
@POST @Consumes(MediaType. APPLICATION_XML ) @Path("/adduser")
Response addUser(User user);
@DELETE @Consumes(MediaType. APPLICATION_XML ) @Path("/deleteuser/{userId}")
Response deleteUser( @PathParam("userId") String userId);
@PUT @Path("/category") @Consumes(MediaType. APPLICATION_XML )
Response updateUser(User user);
@GET @Path("/getusers/startdate/{startDate}/enddate/{endDate}") @Produces({ MediaType. APPLICATION_XML })
Users getUsers( @PathParam("startDate") String startDate , @PathParam("endDate") String endDate); }
@Consumes({ MediaType. APPLICATION_XML })
publicinterface UserService
{ @GET @Produces(MediaType. APPLICATION_XML ) @Path("/searchuser/{userId}")
User searchUser( @PathParam("userId") String userId);
@POST @Consumes(MediaType. APPLICATION_XML ) @Path("/adduser")
Response addUser(User user);
@DELETE @Consumes(MediaType. APPLICATION_XML ) @Path("/deleteuser/{userId}")
Response deleteUser( @PathParam("userId") String userId);
@PUT @Path("/category") @Consumes(MediaType. APPLICATION_XML )
Response updateUser(User user);
@GET @Path("/getusers/startdate/{startDate}/enddate/{endDate}") @Produces({ MediaType. APPLICATION_XML })
Users getUsers( @PathParam("startDate") String startDate , @PathParam("endDate") String endDate); }
publicclass
UserServiceImpl
implements
UserService
{ public User searchUser(String userId)
{ User user = UserDAO. searchUser (userId); if (user == null )
{ ResponseBuilder builder = Response. status ( Status. NOT_FOUND ); builder.type("application/xml"); builder.entity("<errorMsg>User with id:" + userId + " can not be found!</errorMsg>"); thrownew WebApplicationException(builder.build()); } else { System. out .println("User with id:" + userId + " is found"); return user; } }
public Response addUser(User user) { User userObj = (User) UserDAO. searchUser (user.getUserId());
if (userObj != null )
{ ResponseBuilder builder = Response. status ( Status. FORBIDDEN ) ; builder.type("application/xml"); builder.entity("<errorMsg>User with id:" + user.getUserId() + " already exists</errorMsg>") ; thrownew WebApplicationException(builder.build()); } else { UserDAO. addUser (user); return Response. ok (user).build(); } }
public Response deleteUser(String userId) { User userObj = (User) UserDAO. searchUser (userId);
if (userObj == null )
{ ResponseBuilder builder = Response. status ( Status. FORBIDDEN ); builder.type("application/xml"); builder.entity("<errorMsg>User with id:" + userId + " is not existed, delettion request is rejected</errorMsg>"); thrownew WebApplicationException(builder.build()); } else { UserDAO. deleteUser (userId); return Response. ok ().build(); } }
public Response updateUser(User user) { User userObj = (User) UserDAO. searchUser (user.getUserId());
if (userObj == null )
{ ResponseBuilder builder = Response. status ( Status. FORBIDDEN ); builder.type("application/xml"); builder.entity("<errorMsg>User with id:" + user.getUserId() + " is not existed, update request is rejected</errorMsg>"); thrownew WebApplicationException(builder.build()); } else { UserDAO. updateUser (user); return Response. ok (user).build(); } }
public Users getUsers(String startDate, String endDate) { List<User> userList = new ArrayList<User>();
ResponseBuilder builder = Response. status (Status. OK ) ; builder.type("application/xml");
try { userList = UserDAO. getUsersByRegDate (startDate, endDate); } catch (Exception e) { e.printStackTrace(); builder = Response. status (S tatus. NOT_ACCEPTABLE ) ; builder.entity("<errorMsg>" + "not accpertable date format for startDate or endDate</errorMsg>"); thrownew WebApplicationException(builder.build()); } if (userList.size() < 1) { builder = Response. status (Status. NOT_FOUND ) ; builder.entity("<errorMsg>no user found registered between" +startDate+ " and " + endDate + "</errorMsg>"); thrownew WebApplicationException(builder.build()); } else { Users users = new Users(); users.setUsers(userList); return users; } }
{ public User searchUser(String userId)
{ User user = UserDAO. searchUser (userId); if (user == null )
{ ResponseBuilder builder = Response. status ( Status. NOT_FOUND ); builder.type("application/xml"); builder.entity("<errorMsg>User with id:" + userId + " can not be found!</errorMsg>"); thrownew WebApplicationException(builder.build()); } else { System. out .println("User with id:" + userId + " is found"); return user; } }
public Response addUser(User user) { User userObj = (User) UserDAO. searchUser (user.getUserId());
if (userObj != null )
{ ResponseBuilder builder = Response. status ( Status. FORBIDDEN ) ; builder.type("application/xml"); builder.entity("<errorMsg>User with id:" + user.getUserId() + " already exists</errorMsg>") ; thrownew WebApplicationException(builder.build()); } else { UserDAO. addUser (user); return Response. ok (user).build(); } }
public Response deleteUser(String userId) { User userObj = (User) UserDAO. searchUser (userId);
if (userObj == null )
{ ResponseBuilder builder = Response. status ( Status. FORBIDDEN ); builder.type("application/xml"); builder.entity("<errorMsg>User with id:" + userId + " is not existed, delettion request is rejected</errorMsg>"); thrownew WebApplicationException(builder.build()); } else { UserDAO. deleteUser (userId); return Response. ok ().build(); } }
public Response updateUser(User user) { User userObj = (User) UserDAO. searchUser (user.getUserId());
if (userObj == null )
{ ResponseBuilder builder = Response. status ( Status. FORBIDDEN ); builder.type("application/xml"); builder.entity("<errorMsg>User with id:" + user.getUserId() + " is not existed, update request is rejected</errorMsg>"); thrownew WebApplicationException(builder.build()); } else { UserDAO. updateUser (user); return Response. ok (user).build(); } }
public Users getUsers(String startDate, String endDate) { List<User> userList = new ArrayList<User>();
ResponseBuilder builder = Response. status (Status. OK ) ; builder.type("application/xml");
try { userList = UserDAO. getUsersByRegDate (startDate, endDate); } catch (Exception e) { e.printStackTrace(); builder = Response. status (S tatus. NOT_ACCEPTABLE ) ; builder.entity("<errorMsg>" + "not accpertable date format for startDate or endDate</errorMsg>"); thrownew WebApplicationException(builder.build()); } if (userList.size() < 1) { builder = Response. status (Status. NOT_FOUND ) ; builder.entity("<errorMsg>no user found registered between" +startDate+ " and " + endDate + "</errorMsg>"); thrownew WebApplicationException(builder.build()); } else { Users users = new Users(); users.setUsers(userList); return users; } }
D.spring集成restful webService
<beans xmlns=
"http://www.springframework.org/schema/beans"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs= "http://cxf.apache.org/jaxrs"
xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://camel.apache.org/schema/osgi http://camel.apache.org/schema/osgi/camel-osgi.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd" >
<import resource= "classpath:META-INF/cxf/cxf.xml" />
<jaxrs:server id= "userRestService" address= "/userservices/v1.0" >
<jaxrs:serviceBeans>
<ref bean= "userServiceImpl" />
</jaxrs:serviceBeans>
</jaxrs:server>
<bean id= "userServiceImpl" class= "com.netprovider.user.service.baseinfo.impl.UserServiceImpl" > </bean> </beans>
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs= "http://cxf.apache.org/jaxrs"
xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://camel.apache.org/schema/osgi http://camel.apache.org/schema/osgi/camel-osgi.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd" >
<import resource= "classpath:META-INF/cxf/cxf.xml" />
<jaxrs:server id= "userRestService" address= "/userservices/v1.0" >
<jaxrs:serviceBeans>
<ref bean= "userServiceImpl" />
</jaxrs:serviceBeans>
</jaxrs:server>
<bean id= "userServiceImpl" class= "com.netprovider.user.service.baseinfo.impl.UserServiceImpl" > </bean> </beans>
E. 客户端调用
1.
添加:post请求
private static void testAddUser()
{ WebClient client = WebClient.create(SERVICE_URL);
client.path("/adduser").accept(MIME_TYPE).type(MIME_TYPE); User user = new User(); user.setUserId("00" + new Random().nextInt(10000)); user.setGender("Male"); user.setNickname("Who am I"); user.setRegisterDate(new Date()); User addedUser = client.post(user, User.class); System.out.println("added user id:" + addedUser.getUserId()); assertResult(user.getUserId(),addedUser.getUserId()); //set TEST_USER_ID to the one added, so that the user //which is added for testing can be deleted later TEST_USER_ID = addedUser.getUserId(); System.out.println("Test addUser method successfully");}
2.查询:get请求 private static void testGetUsers() {
WebClient client = WebClient.create(SERVICE_URL); //according to the tested data we insert,during this time period //only one person registered
Users users = client.path("/getusers/startdate/2012-10-17/ enddate/2012-10-19") .accept(MIME_TYPE).type(MIME_TYPE).get(Users.class);
assertResult("1", String.valueOf(users.getUsers().size())); System.out.println("Test getUsers method successfully");}
3.修改:put请求 private static void testUpdateUser() {
WebClient client = WebClient.create(SERVICE_URL); User user = new User(); user.setUserId(TEST_USER_ID); user.setGender("Female"); user.setNickname("Test Nickname"); user.setRegisterDate(new Date()); Response response = client.path("/updateuser") .accept(MIME_TYPE).type(MIME_TYPE).put(user); assertResult("200", String.valueOf(response.getStatus())); System.out.println("Test updateUser method successfully"); }
4.删除:delete请求 private static void testDeleteUser() {
WebClient client = WebClient.create(SERVICE_URL); Response response = client.path("/deleteuser/" + TEST_USER_ID) .accept(MIME_TYPE).type(MIME_TYPE).delete(); assertResult("200", String.valueOf(response.getStatus())); System.out.println("Test getUsers method successfully");} private static void assertResult(String expectedResult , String realResult) { if (expectedResult != realResult && !expectedResult.equals(realResult)) { throw new RuntimeException("Test failed,excepted result:" + expectedResult + " real returned result:" + realResult); } }}
private static void testAddUser()
{ WebClient client = WebClient.create(SERVICE_URL);
client.path("/adduser").accept(MIME_TYPE).type(MIME_TYPE); User user = new User(); user.setUserId("00" + new Random().nextInt(10000)); user.setGender("Male"); user.setNickname("Who am I"); user.setRegisterDate(new Date()); User addedUser = client.post(user, User.class); System.out.println("added user id:" + addedUser.getUserId()); assertResult(user.getUserId(),addedUser.getUserId()); //set TEST_USER_ID to the one added, so that the user //which is added for testing can be deleted later TEST_USER_ID = addedUser.getUserId(); System.out.println("Test addUser method successfully");}
2.查询:get请求 private static void testGetUsers() {
WebClient client = WebClient.create(SERVICE_URL); //according to the tested data we insert,during this time period //only one person registered
Users users = client.path("/getusers/startdate/2012-10-17/ enddate/2012-10-19") .accept(MIME_TYPE).type(MIME_TYPE).get(Users.class);
assertResult("1", String.valueOf(users.getUsers().size())); System.out.println("Test getUsers method successfully");}
3.修改:put请求 private static void testUpdateUser() {
WebClient client = WebClient.create(SERVICE_URL); User user = new User(); user.setUserId(TEST_USER_ID); user.setGender("Female"); user.setNickname("Test Nickname"); user.setRegisterDate(new Date()); Response response = client.path("/updateuser") .accept(MIME_TYPE).type(MIME_TYPE).put(user); assertResult("200", String.valueOf(response.getStatus())); System.out.println("Test updateUser method successfully"); }
4.删除:delete请求 private static void testDeleteUser() {
WebClient client = WebClient.create(SERVICE_URL); Response response = client.path("/deleteuser/" + TEST_USER_ID) .accept(MIME_TYPE).type(MIME_TYPE).delete(); assertResult("200", String.valueOf(response.getStatus())); System.out.println("Test getUsers method successfully");} private static void assertResult(String expectedResult , String realResult) { if (expectedResult != realResult && !expectedResult.equals(realResult)) { throw new RuntimeException("Test failed,excepted result:" + expectedResult + " real returned result:" + realResult); } }}
F.客户端配置和spring集成
<?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">
- <context:annotation-config/>
- <import resource="applicationContext-cxf-server.xml"/>
- <context:component-scan base-package="jaxRs" />
- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
- <bean id="webClient" class="org.apache.cxf.jaxrs.client.WebClient" factory-method="create">
- <constructor-arg type="java.lang.String" value="http://localhost:8080/webservice/RESTWebService/" />
- </bean>
- <bean id="rESTSample" class="org.apache.cxf.jaxrs.client.JAXRSClientFactory" factory-method="create">
- <constructor-arg type="java.lang.String" value="http://localhost:8080/webservice/RESTWebService/" />
- <constructor-arg type="java.lang.Class" value="jaxRs.RESTSample" />
- </bean>
- </beans>
G. 注释(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 类型。
@Consumes:声明该方法使用 HTML FORM。
@FormParam:注入该方法的 HTML 属性确定的表单输入。
@Response.created(uri).build(): 构建新的 URI 用于新创建的联系人(/contacts/{id})并设置响应代码(201/created)。