webservice--CXF发布REST的服务

    xiaoxiao2021-03-26  27

    CXF发布REST的服务

    1、什么是REST

    l  定义:REST就是一种编程风格,它可以精确定位网上资源(服务接口、方法、参数)

    l  REST支持数据格式:XML、JSON

    l  REST支持发送方式:GET,POST

    2、需求

    l  第一个:查询单个学生

    l  第二个:查询多个学生

    3、实现

    服务端:

    开发步骤:

    第一步:导入jar包

    第二步:创建学生pojo类,要加入@ XmlRootElement

    package cn.itcast.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.itcast.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.itcast.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.itcast.ws.rest.server; import java.util.ArrayList; import java.util.Date; import java.util.List; import cn.itcast.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; } }

    第五步:发布服务, JAXRSServerFactoryBean发布服务,3个参数,1:服务实现类;2.设置资源类;3.设置服务地址

    package cn.itcast.ws.rest.server; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; import cn.itcast.ws.rest.pojo.Student; /** * * <p>Title: StudentServer.java</p> * <p>Description:服务端</p> */ public class StudentServer { public static void main(String[] args) { //JAXRSServerFactoryBean发布REST的服务 JAXRSServerFactoryBean jaxRSServerFactoryBean = new JAXRSServerFactoryBean(); //设置服务实现类 jaxRSServerFactoryBean.setServiceBean(new StudentInterfaceImpl()); //设置资源类,如果有多个资源类,可以以“,”隔开。 jaxRSServerFactoryBean.setResourceClasses(StudentInterfaceImpl.class); //设置服务地址 jaxRSServerFactoryBean.setAddress("http://127.0.0.1:12345/user"); //发布服务 jaxRSServerFactoryBean.create(); } }

    第六步:测试服务

    http://127.0.0.1:12345/user/student/query/110查询单个学生,返回XML数据

    <student> <birthday>2015-11-27T15:22:14.240+08:00</birthday> <id>110</id> <name>张三</name> </student>

    http://127.0.0.1:12345/user/student/queryList/110?_type=json 查询多个学生,返回JSON

    {"student":[{"birthday":"2015-11-27T15:24:21.565+08:00","id":110,"name":"张三"},{"birthday":"2015-11-27T15:24:21.565+08:00","id":120,"name":"李四"}]}

    http://127.0.0.1:12345/user/student/queryList/110?_type=xml  查询多个学生,返回XML

    <students> <student> <birthday>2015-11-27T15:30:33.754+08:00</birthday> <id>110</id> <name>张三</name> </student> <student> <birthday>2015-11-27T15:30:33.754+08:00</birthday> <id>120</id> <name>李四</name> </student> </students>

    如果服务端发布时指定请求方式是GET(POST),客户端必须使用GET(POST)访问服务端,否则会报如下异常

    如果在同一方法上同时指定XML和JSON媒体类型,在GET请求下,默认返回XML,在POST请求下,默认返回JSON

    客户端: 直接通过url拿到数据进行解析即可

    可以自学一下httpclient

    http://hc.apache.org/httpclient-3.x/

    package cn.itcast.cxf.client; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; /** * * <p>Title: HttpClient.java</p> * <p>Description:HttpURLConnection调用方式</p> * <p>Company: www.itcast.com</p> * @author 传智.at * @date 2015年11月26日下午3:58:57 * @version 1.0 */ public class HttpClient { public static void main(String[] args) throws IOException { //第一步:创建服务地址,不是WSDL地址 URL url = new URL("http://127.0.0.1:12345/user/student/query/110"); //第二步:打开一个通向服务地址的连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //第三步:设置参数 //3.1发送方式设置:POST必须大写 connection.setRequestMethod("POST"); //3.2设置数据格式:content-type //3.3设置输入输出,因为默认新创建的connection没有读写权限, connection.setDoInput(true); //第五步:接收服务端响应,打印 int responseCode = connection.getResponseCode(); if(200 == responseCode){//表示服务端响应成功 InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String temp = null; while(null != (temp = br.readLine())){ sb.append(temp); } System.out.println(sb.toString()); //dom4j解析返回数据 is.close(); isr.close(); br.close(); } } }
    转载请注明原文地址: https://ju.6miu.com/read-661286.html

    最新回复(0)