您的位置:首页 > 其它

webService学习笔记 --- day02rest风格

2016-09-03 11:47 274 查看

一:什么是REST服务

1.REST是一种编程的风格,作用是可以精确定位网上的资源(接口、方法、参数),效率高

2.REST支持的数据格式:JSON,XML

3. REST支持的发送方式:GET,POST

二:发布REST服务

1.开发步骤

(1.) 引入jar包



(2.) 创建实体类
●  注意:类上加入注解:@XMLRootElement,该注解表示java对象和XML数据之间可以转换

package com.cxf.rest.pojo;

import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public 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接口
●  加入注解:@WebService

package com.cxf.rest.service;

import java.util.List;

import javax.jws.WebService;
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 {

//查询单个学生
@POST//指定请求方式,服务端如果发布时指定为GET(POST),客户端访问时必须使用GET(POST)
@Produces(MediaType.APPLICATION_XML)//指定媒体类型,XML
@Path("/query/{id}")//@Path("/query/{id}")的意思是将请求路径中的/query映射到方法上,/{id}映射到对应的参数上,如果有多个参数以/隔开加入到{}
public Student query(@PathParam("id")Integer id);

//查询多个学生
@POST//指定请求方式,服务端如果发布时指定为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 {

@Override
public Student query(Integer 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;
}

}


(5.) 发布服务
●  使用JAXRSServerFactoryBean发布rest服务,3个参数:

A、setServiceBean:设置服务实现类

B、setResourceClasses:设置资源类

C、setAddress:设置服务地址

●  crate:发布服务

package com.cxf.rest.server;

import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;

import com.cxf.rest.service.impl.StudentInterfaceImpl;

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();
}
}

(6.) 测试服务是否发布成功
●  查询单个学生,返回XML数据 http://127.0.0.1:12345/user/student/query/110
<student>
<birthday>2016-09-03T12:56:24.143+08:00</birthday>
<id>110</id>
<name>张三</name>
</student>
●  查询多个学生,返回XML数据
http://127.0.0.1:12345/user/student/queryList/110
<students>
<student>
<birthday>2016-09-03T12:58:46.294+08:00</birthday>
<id>110</id>
<name>张三</name>
</student>
<student>
<birthday>2016-09-03T12:58:46.294+08:00</birthday>
<id>120</id>
<name>李四</name>
</student>
</students>●   查询多个学生,指定媒体类型 http://127.0.0.1:12345/user/student/queryList/110?_type=json
{"student":[{"birthday":"2016-09-03T13:00:10.803+08:00","id":110,"name":"张三"},{"birthday":"2016-09-03T13:00:10.803+08:00","id":120,"name":"李四"}]}


●   注意:
如果在同一个方法上同时指定XML和JSON两种格式,在GET请求下默认返回XML数据;在POST情况下默认返回JSON

2.使用httpClient访问服务端

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>
*/
public class HttpClient {

public static void main(String[] args) throws IOException {
//1:创建服务地址
URL url = new URL("http://127.0.0.1:12345/user/student/queryList/110");
//2:打开到服务地址的一个连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//3:设置连接参数
//3.1设置发送方式:POST必须大写
connection.setRequestMethod("POST");
//3.3设置输入输出,新创建的connection默认是没有读写权限的,
connection.setDoInput(true);
//		connection.setDoOutput(true);

//5:接收服务端的响应
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());

is.close();
isr.close();
br.close();
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  webservice+rest