1.1      什么是rest服务

REST 是一种软件架构模式,只是一种风格,rest服务采用HTTP 做传输协议,REST 对于HTTP 的利用实现精确的资源定位。

Rest要求对资源定位更加准确,如下:

非rest方式:http://ip:port/queryUser.action?userType=student&id=001

Rest方式:http://ip:port/user/student/query/001

Rest方式表示互联网上的资源更加准确,但是也有缺点,可能目录的层级较多不容易理解。

REST 是一种软件架构理念,现在被移植到Web 服务上,那么在开发Web 服务上,偏于面向资源的服务适用于REST,REST 简单易用,效率高,SOAP 成熟度较高,安全性较好。

注意:REST 不等于WebService,JAX-RS 只是将REST 设计风格应用到Web 服务开发上。

1.2      发布rest服务

1.2.1     需求:

发布查询学生信息的服务,以json和xml数据格式返回。

1.2.2     pojo

@XmlRootElement(name="student")

public class Student {

private long id;

private String name;

private Date birthday;

1.2.3     SEI

@WebService

@Path("/student")

public interface StudentService {

@GET

@Produces(MediaType.APPLICATION_XML)

@Path("/query/{id}")

public Student queryStudent(@PathParam("id")long id)throws Exception;

@GET

@Produces({"application/json;charset=utf-8",MediaType.APPLICATION_XML})

@Path("/querylist/{id}")

public List<Student> queryStudentList(@PathParam("id")long id)throws Exception;

}

上边代码中:

queryStudent方法以xml格式发布

queryStudentList方法以json和xml两种格式发布。

1.2.4     SEI实现类

public class StudentServiceImpl implements StudentService {

@Override

public Student queryStudent(long id) throws Exception {

Student student = new Student();

student.setId(100000l);

student.setName("张三");

student.setBirthday(new Date());

return student;

}

@Override

public List<Student> queryStudentList(long id) throws Exception {

Student student1 = new Student();

student1.setId(100000l);

student1.setName("李四");

student1.setBirthday(new Date());

Student student2 = new Student();

student2.setId(100000l);

student2.setName("张三");

student2.setBirthday(new Date());

List<Student> list = new ArrayList<Student>();

list.add(student1);

list.add(student2);

return list;

}

}

1.2.5     程序代码发布

public class RestServer {

public static void main(String[] args) {

JAXRSServerFactoryBean jaxrsServerFactoryBean = new JAXRSServerFactoryBean();

//rest地址

jaxrsServerFactoryBean.setAddress("http://127.0.0.1:12345/rest");

//设置SEI实现类

jaxrsServerFactoryBean.setResourceClasses(StudentServiceImpl.class);

jaxrsServerFactoryBean.create();

}

}

1.2.6     Spring配置文件发布

<!-- rest服务发布 -->

<jaxrs:server address="/rest">

<jaxrs:serviceBeans>

<bean class="cn.itcast.ws.cxf.rest.server.StudentServiceImpl"/>

</jaxrs:serviceBeans>

1.2.7     测试

queryStudent方法测试:

http://127.0.0.1:8080/工程名/ws/rest/student/query/1

queryStudentList方法测试:

返回json

http://127.0.0.1:8080/工程名/ws/rest/student/querylist/1?_type=json

返回xml

http://127.0.0.1:8080/工程名/ws/rest/student/querylist/1?_type=xml

最新文章

  1. vtk多线程简单测试
  2. currentColor-CSS3非常有用的变量
  3. ios第二天{函数}
  4. Groovy中文教程(链接收藏)
  5. poj 2240(floyd)
  6. 基于SSH2的OA项目1.1_20161207_业务开发
  7. 移动端HTML5资源整理
  8. 杂谈SharpDx中的WIC组件——我们需要WIC的图片编码功能么?
  9. C# Socket编程(1)基本的术语和概念
  10. jdbc:java数据库连接
  11. 二十、ValueStack的常用方法
  12. Android工程目录及其作用简介
  13. C# 导出一个控件的矢量图
  14. django1.6之template基础用法
  15. Android:控件ListView列表项与适配器结合使用
  16. IIS Web服务扩展中添加ASP.NET4.0
  17. JavaScript中判断鼠标按键(event.button)
  18. Python-接口自动化(一)
  19. IntellJ IDEA2017 springboot2.0.2中读取配置
  20. BZOJ.3489.A simple rmq problem(主席树 Heap)

热门文章

  1. LAMP 1.2 Apache编译安装问题解决
  2. maven仓库的管理_Nexus
  3. mongodb 分页(limit)
  4. 1、linux-wget
  5. CF1042E Vasya and Magic Matrix
  6. latex 输入矩阵
  7. 36.浅谈DLL劫持
  8. 7.23实习培训日志-JDBC
  9. web.config中authorization下的location中的path的设置 (转)
  10. sublime text 3安装及使用