如何封装RESTful Web Service

所谓Web Service是一个平台独立的,低耦合的,自包含的、可编程的Web应用程序,有了Web Service异构系统之间就可以通过XML或JSON来交换数据,这样就可以用于开发分布式的互操作的应用程序。Web Service使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件就可相互交换数据或集成,无论它们各自所使用的语言、平台或内部协议是什么,都可以相互交换数据。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。
  REST(REpresentational State Transfer)是Roy Fielding博士于2000年在他的博士论文中提出来的一种软件 架构风格。它是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。近年来,越来越多的Web Service开始采用REST风格设计和实现。例如, 亚马逊提供接近REST风格的Web Service进行图书查找; 雅虎提供的Web Service也是REST风格的。

这里写图片描述


这里写图片描述

  如果要对REST有更深入的了解和更深刻的认识,推荐大家阅读InfoQ上面的一篇文章《理解本真的REST架构风格》。相信很多自以为懂REST的人看完这篇文章之后才知道什么是真正的REST。在IBM的开发者社区中有一篇非常好的文章,名为《使用Spring 3来创建RESTful Web Services》,讲解如何用spring Web和Spring MVC来创建REST风格的Web Service。由于这篇文章已经讲得很好了,这里我就不再赘述其中的内容。

  这里要讲的是基于Apache CXF来创建RESTful Web Service。可以在Apache的网站下载到CXF的发行版本。Apache的官网是这样介绍CXF的:

Apache CXF is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.

  下载完成后解压并找到lib目录,将其中的jar文件添加你的Java项目中,接下来就可以开始编写你的Web Service程序了。话不多说,直接上代码。

package com.lovo.domain;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Student")
public class Student {  
    private Integer id;
    private String name;
    private String birthday;

    public Student() {
    }

    public Student(Integer id, String name, String birthday) {
        this.id = id;
        this.name = name;
        this.birthday = 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 String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
package com.lovo.infrastructure;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lovo.domain.Student;

public class StudentRepository {
    private Map<Integer, Student> map = new HashMap<>();

    public StudentRepository() {
        map.put(1001, new Student(1001, "骆昊", "1980-11-28"));
        map.put(1002, new Student(1002, "王大锤", "1992-2-2"));
        map.put(1003, new Student(1003, "张三丰", "1930-3-3"));
    }

    public void save(Student student) {
        if (student != null) {
            map.put(student.getId(), student);
        }
    }

    public void delete(Student student) {
        if (student != null && map.containsKey(student.getId())) {
            map.remove(student.getId());
        }
    }

    public void update(Student student) {
        delete(student);
        save(student);
    }

    public Student findById(Integer id) {
        return map.get(id);
    }

    public List<Student> findAll() {
        return new ArrayList<Student>(map.values());
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
package com.lovo.service;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.lovo.domain.Student;
import com.lovo.infrastructure.StudentRepository;

@Path("/service")
@Produces("application/json")
public class StudentService {
    private StudentRepository studentRepo = new StudentRepository();

    @GET
    @Path("/stu/{id}")
    @Consumes("application/json")
    public Student getStudent(@PathParam("id") Integer id) {
        return studentRepo.findById(id);
    }

    @GET
    @Path("/stu")
    @Consumes("application/json")
    public List<Student> getAllStudents() {
        return studentRepo.findAll();
    }

    @POST
    @Path("/stu")
    @Consumes("application/json")
    public boolean addStudent(Student student) {
        if (getStudent(student.getId()) == null) {
            studentRepo.save(student);
            return true;
        }
        return false;
    }

    @PUT
    @Path("/stu/{id}")
    @Consumes("application/json")
    public boolean updateStudent(@PathParam("id") Integer id, Student student) {
        if (getStudent(id) != null) {
            studentRepo.update(student);
            return true;
        }
        return false;
    }

    @DELETE
    @Path("/stu/{id}")
    @Consumes("application/json")
    public boolean deleteStudent(@PathParam("id") Integer id) {
        Student student = getStudent(id);
        if (student != null) {
            studentRepo.delete(student);
            return true;
        }
        return false;
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

  最后来启动Web Service的服务器并进行测试

package com.lovo;

import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;

import com.lovo.domain.Student;
import com.lovo.service.StudentService;

public class MyRestServer {  

    public static void main(String[] args) {
        JAXRSServerFactoryBean myRESTfulServer = new JAXRSServerFactoryBean();
        myRESTfulServer.setResourceClasses(Student.class);  
        myRESTfulServer.setServiceBean(new StudentService());
        myRESTfulServer.setAddress("http://localhost:9999/");  
        myRESTfulServer.create();  
    }  
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值