webservice--CXF+Spring整合发布REST的服务

    xiaoxiao2021-03-26  29

    CXF+Spring整合发布REST的服务

    服务器

    开发步骤:

    第一步:创建web项目(引入jar包)

    第二步:创建POJO类

     

    package cn.hcx.ws.rest.pojo; import java.util.Date; import javax.xml.bind.annotation.XmlRootElement; /** * * <p>Title: Student.java</p> * <p>Description:学生实体类</p> */ @XmlRootElement(name="student")//@XmlRootElement可以实现对象和XML数据之间的转换 public class Student { private long id; private String name; private Date birthday; public long getId() { return id; } public void setId(long 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; } }  

     

    第三步:创建SEI接口

     

    package cn.hcx.ws.rest.server; 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 cn.hcx.ws.rest.pojo.Student; /** * * <p>Title: StudentInterface.java</p> * <p>Description:学生接口</p> */ @WebService @Path("/student")//@Path("/student")就是将请求路径中的“/student”映射到接口上 public interface StudentInterface { //查询单个学生 @POST//指定请求方式,如果服务端发布的时候指定的是GET(POST),那么客户端访问时必须使用GET(POST) @Produces(MediaType.APPLICATION_XML)//指定服务数据类型 @Path("/query/{id}")//@Path("/query/{id}")就是将“/query”映射到方法上,“{id}”映射到参数上,多个参数,以“/”隔开,放到“{}”中 public Student query(@PathParam("id")long id); //查询多个学生 @GET//指定请求方式,如果服务端发布的时候指定的是GET(POST),那么客户端访问时必须使用GET(POST) @Produces({"application/json;charset=utf-8",MediaType.APPLICATION_XML})//指定服务数据类型 @Path("/queryList/{name}")//@Path("/queryList/{name}")就是将“/queryList”映射到方法上,“{name}”映射到参数上,多个参数,以“/”隔开,放到“{}”中 public List<Student> queryList(@PathParam("name")String name); }  

     

    第四步:创建SEI实现类

     

    package cn.hcx.ws.rest.server; import java.util.ArrayList; import java.util.Date; import java.util.List; import cn.hcx.ws.rest.pojo.Student; /** * * <p>Title: StudentInterfaceImpl.java</p> * <p>Description:学生的实现类</p> */ public class StudentInterfaceImpl implements StudentInterface { @Override public Student query(long id) { Student st = new Student(); st.setId(110); st.setName("张三"); st.setBirthday(new Date()); return st; } @Override public 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; } }  

     

    第五步:配置Spring配置文件,applicationContext.xml,<jaxrs:server,设置1.服务地址;2.服务实现类

    <?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"> <!-- <jaxrs:server发布REST的服务 ,对JAXRSServerFactoryBean类封装--> <jaxrs:server address="/user"> <jaxrs:serviceBeans> <ref bean="studentInterface"/> </jaxrs:serviceBeans> </jaxrs:server> <!-- 配置服务实现类 --> <bean name="studentInterface" class="cn.hcx.ws.rest.server.StudentInterfaceImpl"/> </beans>  

    第六步:配置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的环境 --> <context-param> <!--contextConfigLocation是不能修改的 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <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> <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>  

    第七步:部署到tomcat下,启动tomcat

    第八步:测试服务

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

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

     

    客户端

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript"> function queryStudent(){ //创建XMLHttpRequest对象 var xhr = new XMLHttpRequest(); //打开连接 xhr.open("get","http://127.0.0.1:8080/ws_4_cxf_rest_spring_server/ws/user/student/queryList/110?_type=json",true); //设置回调函数 xhr.onreadystatechange=function(){ //判断是否发送成功和判断服务端是否响应成功 if(4 == xhr.readyState && 200 == xhr.status){ alert(eval("("+xhr.responseText+")").student[0].name); } } //发送数据 xhr.send(null); } </script> </head> <body> <input type="button" value="查询" onclick="javascript:queryStudent();"/> </body> </html>      
    转载请注明原文地址: https://ju.6miu.com/read-662086.html

    最新回复(0)