webservice学习笔记 --- day03 CXF+Spring+REST

来源:互联网 发布:openwrt 阿里云ddns 编辑:程序博客网 时间:2024/06/13 03:17

一:CXF整合Spring发布REST服务

1.开发步骤

(1.)  创建Web项目,引入jar
(2.)  创建实体类
package com.cxf.rest.pojo;import java.util.Date;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElementpublic class Student {private Integer id;private String name;private Date birthday;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}}


(3.)  创建SEI接口
package com.cxf.rest.service;import java.util.List;import javax.jws.WebService;import javax.ws.rs.GET;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import com.cxf.rest.pojo.Student;@WebService@Path("/student")//将请求路径中的"/student"映射到该接口上public interface StudentInterface {//查询单个学生@GET//指定请求方式,服务端如果发布时指定为GET(POST),客户端访问时必须使用GET(POST)@Produces(MediaType.APPLICATION_XML)//指定媒体类型,XML@Path("/query/{id}")//@Path("/query/{id}")的意思是将请求路径中的/query映射到方法上,/{id}映射到对应的参数上,如果有多个参数以/隔开加入到{}public Student query(@PathParam("id")Integer id);//查询多个学生@GET//指定请求方式,服务端如果发布时指定为GET(POST),客户端访问时必须使用GET(POST)@Produces({"application/json;charset=utf-8",MediaType.APPLICATION_XML})//指定媒体类型,JSON@Path("/queryList/{name}")//@Path("/queryList/{name}")的意思是将请求路径中的/queryList映射到方法上,/{name}映射到对应的参数上,如果有多个参数以/隔开加入到{}public List<Student> queryList(@PathParam("name")String name);}


(4.)  创建实现类
package com.cxf.rest.service.impl;import java.util.ArrayList;import java.util.Date;import java.util.List;import com.cxf.rest.pojo.Student;import com.cxf.rest.service.StudentInterface;public class StudentInterfaceImpl implements StudentInterface {@Overridepublic Student query(Integer id) {Student st = new Student();st.setId(110);st.setName("张三");st.setBirthday(new Date());return st;}@Overridepublic List<Student> queryList(String name) {Student st = new Student();st.setId(110);st.setName("张三");st.setBirthday(new Date());Student st2 = new Student();st2.setId(120);st2.setName("李四");st2.setBirthday(new Date());List<Student> list = new ArrayList<Student>();list.add(st);list.add(st2);return list;}}


(5.)  配置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" 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/jaxws http://cxf.apache.org/schemas/jaxws.xsd            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">            <!-- 发布rest服务 -->            <jaxws:server address="/user"><jaxws:serviceBean><ref bean="studentServer"/></jaxws:serviceBean></jaxws:server>                        <!-- 服务实现类 --><bean name="studentServer" class="com.cxf.rest.service.impl.StudentInterfaceImpl"></bean>                        </beans>


(6.)  配置web.xml
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>ws_2_cxf_spring_server</display-name>  <!-- 配置spring的上下文,该配置可以有,也可以步配置,配置之后,在配置的路径下加载,如果不配置,默认的路径是:/WEB-INF/applicationContext.xml -->  <context-param>  <!--  contextConfigLocation名称不能修改,指定spring配置文件地址 -->  <param-name>contextConfigLocation</param-name>  <param-value>classpath:applicationContext.xml</param-value>  </context-param>  <!-- 配置spring的监听器listener -->  <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>    <!-- 配置CXF的servlet -->  <servlet>  <servlet-name>cxf</servlet-name>  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  </servlet>  <servlet-mapping>  <servlet-name>cxf</servlet-name>  <!--   四种映射方式:  1.路径映射:以/开头,以/*结尾  2.扩展映射:以*.结尾,比如*.do,*.action  3.默认映射:直接写/  4.精确映射  映射规则:  1.最高:精确映射  2.次高:路径映射  3.三高:扩展映射  4.最后:默认映射,如果没有配置默认映射,报异常   -->  <url-pattern>/ws/*</url-pattern>  </servlet-mapping>    <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list></web-app>

(7.)  部署启动tomcat
(8.)  测试服务是否发布成功

REST服务的使用说明书地址:

http://127.0.0.1:8080/ws_4_cxf_rest_spring_server/ws/user?_wadl

服务地址规则:

http://ip:端口号/项目名称/servlet拦截路径/服务名称/接口映射/方法映射/参数映射



0 0
原创粉丝点击