之前项目中用CXF+Spring可以搭建WebService服务,后面发现cxf可以支持Restful规范的接口。于是,在接下去的练手项目中,打算用cxf+spring来搭建一个Restful接口服务。
战斗前的准备
我是在官网下的cxf最新版本,已经包含了spring的jar包。所以,我先把一些明确不会用到的jar包去除后,其他的全部导入了。
配置web.xml文件
主要是配置spring和cxf。
<!-- spring 监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml,classpath*:com/*/*Context.xml,classpath*:com/*/*/*Context.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>/web/*</url-pattern>
</servlet-mapping>
添加spring配置文件
新建一个applicationContext.xml(这边名字需要能与web.xml中配置的监听匹配)
<