webservice+restful+cxf(服务端客户端调用)
2015-11-25 00:00
555 查看
摘要: 经过漫长的时间周期,终于完成了,restful风格的webservice开发工作,在这里记录一下学习过程以及新的体会
1.web.xml
2.rest.xml
3.java
4.直接访问路径 其中ws是web.xml中的配置,rest是rest.xml文件中的配置,demo是项目path路径
(1)直接显示发布接口
http://192.168.28.101:8080/project/ws
![](http://static.oschina.net/uploads/space/2015/1125/182825_y3S7_1169736.png)
(2)直接访问资源,直接得到 demo资源下list的数据并且用xml格式返回
http://192.168.28.101:8080/project7.0/ws/rest/demo/list?_type=xml //同样可以使用_type=json返回数据
![](http://static.oschina.net/uploads/space/2015/1125/183110_xIto_1169736.png)
5.上边都是服务端发布,接下来是客户端调用
6.针对返回json数据显示格式调整
7.针对restful拦截器使用
大工搞成,下边可以下载
链接:http://pan.baidu.com/s/1i3LdDGD 密码:4rqe
1.web.xml
<servlet> <servlet-name>CXFServlet</servlet-name> <display-name>CXF Servlet</display-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>/ws/*</url-pattern> </servlet-mapping>
2.rest.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:cxf="http://cxf.apache.org/core" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <jaxrs:server id="restServiceContainer" address="/rest"> <jaxrs:serviceBeans> <ref bean="restSample" /> <ref bean="restVuln" /> </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> </beans>
3.java
@Path(value = "/sample") public interface RESTSample { @GET @Produces(MediaType.TEXT_PLAIN) @Path("/test/{param}") public String doRequestTest(@PathParam("param") String param, @Context HttpServletRequest servletRequest, @Context HttpServletResponse servletResponse);
4.直接访问路径 其中ws是web.xml中的配置,rest是rest.xml文件中的配置,demo是项目path路径
(1)直接显示发布接口
http://192.168.28.101:8080/project/ws
![](http://static.oschina.net/uploads/space/2015/1125/182825_y3S7_1169736.png)
(2)直接访问资源,直接得到 demo资源下list的数据并且用xml格式返回
http://192.168.28.101:8080/project7.0/ws/rest/demo/list?_type=xml //同样可以使用_type=json返回数据
![](http://static.oschina.net/uploads/space/2015/1125/183110_xIto_1169736.png)
5.上边都是服务端发布,接下来是客户端调用
private static WebClient client; @Before public void init() { // 手动创建webClient对象,注意这里的地址是发布的那个/rest地址 String url = "http://192.168.28.101:8080/project/ws/rest/"; client = WebClient.create(url); // 从Spring Ioc容器中拿webClient对象 // ApplicationContext ctx = new ClassPathXmlApplicationContext("WEB-INF/applicationContext-client.xml"); // client = (WebClient) ctx.getBean("webClient", WebClient.class); } @After public void destory(){ } //自己�??发的 @Test public void testGet() { try { System.out.println(client.path("sample").accept(MediaType.TEXT_PLAIN).get(String.class)); } catch (Exception e) { e.printStackTrace(); } } @Test public void testRequest() { System.out.println(client.path("sample/request/234234").accept(MediaType.TEXT_PLAIN).get(String.class)); } @Test public void testBean() { User user = client.path("sample/bean/{id}", 25).accept(MediaType.APPLICATION_XML).get(User.class); System.out.println(user); } @Test public void testList() { System.out.println("--------------"+client.path("sample/list").accept(MediaType.APPLICATION_XML).get(Users.class).getUsers()); }
6.针对返回json数据显示格式调整
<bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider"> <property name="serializeAsArray" value="true" /> <property name="arrayKeys" ref="jsonKeys" /> <property name="produceMediaTypes" ref="jsonTypes" /> <property name="consumeMediaTypes" ref="jsonTypes" /> <property name="ignoreNamespaces" value="true" /> <property name="dropRootElement" value="true" /> <property name="ignoreMixedContent" value="true" /> <property name="supportUnwrapped" value="true" /> <property name="attributesToElements" value="true" /> </bean>
7.针对restful拦截器使用
<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>
大工搞成,下边可以下载
链接:http://pan.baidu.com/s/1i3LdDGD 密码:4rqe
相关文章推荐
- qd
- c# .net开发工程师
- HTML基本介绍
- Oracle逻辑结构
- Oracle基本操作
- jq判断节点是否存在某个事件
- 阻止事件冒泡
- setTimeout 和 setInterval最小执行时间问题
- 毛玻璃
- MySql批量数据导入Load data infile解决方案
- windows下面安装pip
- OpenDNS(转)
- 1、下载安装scala编译器(可以理解为scala的jdk),地址:http://www.scala
- 为什么scala中main方法必须放在object中
- scala中for循环(一)
- scala中for循环(二)
- scala中until方法
- scala中99乘法表与java中99乘法表对比
- scala中函数调用
- scala中默认擦描述和带名参数