CXF发布restful接口

来源:互联网 发布:linux 可执行文件 编辑:程序博客网 时间:2024/05/09 18:16

参考:http://www.cnblogs.com/hoojo/archive/2012/07/23/2605219.html

上述文章已经能够阐述出发布restful接口的问题,这里我只是说说快捷的建立方式。

其实发布一个webService接口,除了soap技术,cxf结合spring开发restful接口是很方便的,十分钟就可以搞定。

话不多说,步骤:

1.建立一个web工程,添加jar包

这只是一部分jar包,具体的jar包就自己搞定了。

工程结构如下图:



2.前边的准备工作完成了接着

建立自己的pojo类,写get/set方法,这些都是老规矩,唯一不同的是类里使用@XmlRootElement注解来暴露你的pojo对象:


接着是dao层;因为是个test案例,并没有连接数据库,dao层就简单返回个字符串就行,代码我就省了,相信这儿都没什么问题

接着主要的活儿就来了。

service及其实现类

实现类与我们平时的实现类没啥区别,就是调dao层的方法获得/更改/删除/查询数据。

service层采用注解的方式,先给大家po图再解释注解的意思:

service层我就写了一个方法,getWords();,有两个参数,其实注解很简单

@Path() 就是路径

@GET/@POST就是请求方式

@PathParam就是绑定参数

@Produces 就是发送的数据格式

@Consumes 就是接收的数据格式

。。。这些注解还有好多,可以参考网上的。使用起来如同SpringMVC方便

唯一值得注意的就是get的参数是跟在url上的 post传参数的话是json格式的data,如果是在实际开发中,比如是开发一个andriod app,要在界面发送ajax post请求访问接口

提交数据的话,需要注意的是一定要使用JSON.stringify(param),将对象转json字符串发送,另外还有跨域的问题,就是使用jsonp这些与本文无关

service层就是这么简单的一些接口方法,实现类我就省了,并没有什么特别的


现在就是配置几个xml文件了;上一个最简单且通俗易懂的版本:

webxml:

<?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"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">  <display-name>test</display-name>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext.xml</param-value>   <param-value>classpath:spring-ws-restful.xml</param-value>  </context-param>    <listener>            <listener-class>                org.springframework.web.context.ContextLoaderListener            </listener-class>       </listener>    <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>/*</url-pattern>  </servlet-mapping>    <filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

这个很简单  就配置了CXFServlet,加了个ContextLoaderListener,其实这个Listener才是启动cxfservlet的关键,所以必须要有,在springMVC结合cxf发布接口的时候

就会因为这个contextloadlistener冲突,当然这个也不是今天的主要内容

接下来是:

<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xmlns:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"><context:component-scan base-package="service" /><context:component-scan base-package="dao" /><context:component-scan base-package="pojo" /><context:component-scan base-package="client" /></beans>


就是配置了个注解

接下来:

<?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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"xmlns:jaxws="http://cxf.apache.org/jaxws"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.xsdhttp://cxf.apache.org/corehttp://cxf.apache.org/schemas/core.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/jaxrshttp://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="userServiceImpl" class="service.impl.userServiceImpl" ></bean><jaxrs:server id="userSerivce" address="/userService"><jaxrs:serviceBeans><ref bean="userServiceImpl" /></jaxrs:serviceBeans><jaxrs:extensionMappings><entry key="json" value="application/json" /></jaxrs:extensionMappings><jaxrs:languageMappings><entry key="en" value="en-gb" /></jaxrs:languageMappings></jaxrs:server></beans>


这里面的东西一个不能少,只可以多。其实就是配置服务及其实现,使其暴露


现在就可以看看结果了,把项目运行在tomcat上之后,输入http://localhost:8888/test试试

我的结果是这样的:

接口就发布成功了!是不是就10分钟搞定!

接下来写个Client来测试一下:

package client;import org.apache.cxf.jaxrs.client.WebClient;public class webClientTest {public static void main(String[] args) {test();}public static void test(){try {String url="http://127.0.0.1:8888/test/userService/user";String path="/getWods/{username}/{pwd}";WebClient client=WebClient.create(url).accept("application/json").type("application/json");client.path(path,"test","123");String result=client.get(String.class);System.out.println(result);} catch (Exception e) {e.printStackTrace();System.out.println(e.toString());}}}

运行。看控制台输出:


下一篇准备讲讲爬虫







0 0
原创粉丝点击