一、接口类
<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