CXF_Spring_Rest
一、接口类
<PRE class=java name="code">@Path("/rest_HelloWorld")
public interface Rest_HelloWorld {
@GET
@Produces (MediaType.TEXT_PLAIN)
@Path("/say/{name}")
public String say(@PathParam("name")String name); @GET
@Produces (MediaType.TEXT_PLAIN)
@Path("/sayHello/{name}")
public String sayHello(@PathParam("user")User user); @GET
@Produces ({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Path("/getList/{id}")
public List<User> getList(@PathParam("id")Long id); }</PRE>
(1)、@Path,标注资源类或方法的相对路径
(2)、@GET,@PUT,@POST,@DELETE,标注方法是用的HTTP请求的类型
GET 列出资源集合的所有成员。
PUT 使用一个集合更新(替换)另一个集合。
POST 在集合中创建数字资源,
DELETE 删除整个资源集合。
(3)、@Produces,标注返回的MIME媒体类型,( 注解标注,这个注解可以包含一组字符串,默认值是*/*,它指定REST 服务的响应结果的MIME 类型,例如:application/xml、application/json、image/jpeg 等),你也可以同时返回多种类型,但具体生成结果时使用哪种格式取决于ContentType。
CXF 默认返回的是JSON 字符串。
(4)、@PathParam,@QueryParam,@HeaderParam,@CookieParam,@MatrixParam,@FormParam,分别标注方法的参数来自于HTTP请求的不同位置,例如@PathParam来自于URL的路径,@QueryParam来自于URL的查询参数,@HeaderParam来自于HTTP请求的头信息,@CookieParam来自于HTTP请求的Cookie。
2、接口实现类
@Service("rest_HelloWorldImpl")
public class Rest_HelloWorldImpl implements Rest_HelloWorld { public String say(String name) {
return name+",您好!";
} public String sayHello(User user) {
return user.getName()+",您好!";
} public List<User> getList(Long id){
List<User> list = new ArrayList<User>(); Long sid=1L;
User user = new User(sid,"张三"+sid,21);
list.add(user); sid=2L;
user = new User(sid,"张三"+sid,21);
list.add(user); sid=3L;
user = new User(sid,"张三"+sid,21);
list.add(user);
return list;
}
}
3、CXF在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:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.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="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
<bean id="outMessageInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> <!--id:名称(随意配),implementor:指定接口具体实现类,address:随意配-->
<jaxws:endpoint id="helloWorld" implementor="#HelloWorldImpl" address="/HelloWorld" >
<!-- 输入日志拦截器 -->
<jaxws:inInterceptors>
<ref bean="inMessageInterceptor"/>
</jaxws:inInterceptors>
<!-- 输出日志拦截器 -->
<jaxws:outInterceptors>
<ref bean="outMessageInterceptor"/>
</jaxws:outInterceptors>
<jaxws:properties>
<entry key="mtom_enabled" value="true"></entry>
</jaxws:properties>
</jaxws:endpoint> <jaxrs:server id="rest_HelloWorld" address="/">
<jaxrs:inInterceptors>
<ref bean="inMessageInterceptor"/>
</jaxrs:inInterceptors>
<jaxrs:outInterceptors>
<ref bean="outMessageInterceptor"/>
</jaxrs:outInterceptors>
<jaxrs:serviceBeans>
<ref bean="rest_HelloWorldImpl" />
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</jaxrs:extensionMappings>
<jaxrs:languageMappings>
<entry key="en" value="en-gb" />
</jaxrs:languageMappings>
</jaxrs:server> <!-- WebService 客户端 spring 配置文件cxf与Spring集成,cxf里提供了一个工厂类org.apache.cxf.jaxws.JaxWsProxyFactoryBean,
可以方便实现的调用WebService。serviceClass属性是接口类,address是webService的路径在其他bean里如果要调用webservice,
只要将client这个bean注入到需要使用的bean里。-->
<bean id="client" class="com.exp.service.outer.HelloWorld" factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.exp.service.outer.HelloWorld" />
<property name="address" value="http://localhost:8080/demo/webservice/HelloWorld" />
</bean>
</beans>
4、访问
http://localhost:8080/demo/webservice/rest_HelloWorld/getList/1
返回JSON格式: http://localhost:8080/demo/webservice/rest_HelloWorld/getList/1?_type=json
返回XML格式: http://localhost:8080/demo/webservice/rest_HelloWorld/getList/1?_type=xml
5、资料:
http://www.ibm.com/developerworks/cn/web/wa-aj-tomcat/
(clience)http://alvinalexander.com/java/java-apache-httpclient-restful-client-examples
CXF_Spring_Rest的更多相关文章
随机推荐
- Manacher 最长回文子串。
最长回文子串就是一个字符串的一个子串,他从左往右读和从右往左读是一样的. 可以用 Manacher 算法来求,他的复杂度是 O(n) . 可以看这篇文章 http://blog.csdn.net/yw ...
- kafka第三篇--安装使用
说明:直接下载二进制包可省略安装过程,省略很多麻烦. 1单机 安装 安装过程,参考官网: > tar xzf kafka-<VERSION>.tgz > cd kafka-&l ...
- mustache.js使用基本(三)
作者:zccst 本节要点是子模块(partials)和分隔符(delimiter)等 1,子模块(partials) /* {{>partials}}以>开始表示子模块,如{{> ...
- Objective-c学习笔记3
objective-c代码块多并发 1.代码块对象是对C语言中函数的扩展,除了函数中的代码,代码块还包含有变量绑定,代码块有时也被称为闭包 2.代码块包含两种绑定类型,自动绑定使用的是栈空间,托管绑定 ...
- Spring自学教程-ssh整合(六)
以下是本人原创,如若转载和使用请注明转载地址.本博客信息切勿用于商业,可以个人使用,若喜欢我的博客,请关注我,谢谢!博客地址 感谢您支持我的博客,我的动力是您的支持和关注!如若转载和使用请注明转载地址 ...
- POJ 3991 Seinfeld
首先进行一次括号匹配,把正确匹配的全部删去. 删去之后剩下的状态肯定是 L个连续右括号+R个连续左括号. 如果L是偶数,答案是L/2+R/2: 否则答案是 (L-1)/2+(R-1)/2+2: #in ...
- ASIHTTPRequest异步请求
我们运行程序,如果网速很慢,查询的时候会一直黑屏,直到请求结束画面才出现,这样用户体验很不好.因此同步请求一般只是在某个子线 程中使用,而不在主线程中使用.异步请求的用户体验要比同步请求好,因此一般情 ...
- 浅析IoC框架
今日拜读了一篇关于IOC的文章,特意转载,和大家分享一下 1 IoC理论的背景 我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实 ...
- STM32中的位带(bit-band)操作(转)
源:STM32中的位带(bit-band)操作 支持了位带操作后,可以使用普通的加载/存储指令来对单一的比特进行读写.在 CM3 中,有两个区中实现了位带.其中一个是 SRAM 区的最低 1MB 范围 ...
- 74HC166与TPIC6A595分析(转)
源:Atmega162串行外设接口SPI 一.Atmega162的SPI接口基本概念与工作原理 SPI接口的全称是"Serial Peripheral Interface",意为串 ...