`

Restful架构服务构建指南

    博客分类:
  • J2EE
阅读更多

REST定位为“分布式超媒体应用(Distributed Hypermedia System)”的架构风格。REST是“REpresentational State Transfer”的缩写,可以翻译成“表现状态转换”,但是在绝大多数场合中我们只说REST或者RESTful。


REST,即Representational State Transfer的缩写。RESTful架构,是目前最流行的一种互联网软件架构。它结构清晰、符合标准、易于理解、扩展方便,基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制,所以正得到越来越多网站的采用。如果一个架构符合REST原则,就称它为RESTful架构。


Web 应用程序最重要的 REST 原则是,客户端和服务器之间的交互在请求之间是无状态的。从客户端到服务器的每个请求都必须包含理解请求所必需的信息。

 

  • 资源(resource):网络上的一个实体或者说是一个具体信息,可以是一段文本、一张图片、一首歌曲、一种服务。
  • 统一资源定位符(URI,Universal Resource Identifier):一个资源的识别符或者说是一个地址,通过URI你可以定位到特定的资源。要获取这个资源,需要访问它的URI,因此,URI就成了每一个资源的地址或独一无二的识别符。
  • 状态转换(State Transfer): 所有资源都共享统一的接口,以便在客户端和服务器之间传输状态。客户端与服务器互动的过程,通常涉及到服务器端数据和状态的变化过程,比如文件被修改,访问数量增加等。

REST的使用场景是Machine-to-machine的系统集成,目标是让服务发布者和消费者在最小约束下自由演化。这个约束是指服务契约,简单讲就是服务输入输出的语义。消费者只需知道服务的根资源的URI,就可以由根资源引导到所需的资源。换句话说,消费者和发布者的耦合只在于根资源的URI以及各资源及其操作的语义。

rest服务的优势:
1.服务自解释
2.降低服务的版本粒度
3.降低消费者对服务内部实现细节的耦合
4.数据描述简单,一般以xml,json做数据交换。

 

以前我们可能需要纠结于依赖底层系统的接口对象版本,接口的设计需要充分且谨慎考虑依赖版本的兼容性问题并进行审慎评估,而版本的升级又带来客户端系统的再次发布问题。现在rest服务则彻底释放了,客户端不需要在被服务端系统绑架进行所谓依赖版本的升级,关注度可以更加聚焦与客户端系统的实际业务处理。

 

下面开始构建一个rest服务:

 

1、生成maven工程,执行以下命令

mvn archetype:generate -DgroupId=com.myteay -DartifactId=rest-common-persist -Dversion=1.0.0 -Dpackage=com.myteay -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeGroupId=org.apache.maven.archetypes  -DinteractiveMode=false 

 

2、编辑pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.myteay</groupId>
  <artifactId>rest-common-persist</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <name>rest-common-persist</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
  </build>

  <repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
  </pluginRepositories>

</project>

 

 

3、生成eclipse导入工程,执行: mvn eclipse:eclipse

 

 

4、编写rest服务的controller

/**
 * Danlley Wei (mailto://danlley@126.com)
 * Copyright (c) 2005-2017 All Rights Reserved.
 */
package com.myteay.restful.service.impl;

import java.util.Random;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * @author danlley(danlley@126.com)
 * @version $Id: RestfulController.java, v 0.1 2017年4月13日 下午11:05:43 danlley(danlley@126.com) Exp $
 */
@RestController
public class RestfulController {

    @RequestMapping("/greetting")
    public ObjectModel greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        System.out.println("-------------------------------->" + name);
        return new ObjectModel(new Random(100).nextInt(), name);
    }
}

 

5、编写实体类

/**
 * Danlley Wei (mailto://danlley@126.com)
 * Copyright (c) 2005-2017 All Rights Reserved.
 */
package com.myteay.restful.service.impl;

/**
 * @author danlley(danlley@126.com)
 * @version $Id: ObjectModel.java, v 0.1 2017年4月13日 下午11:09:21 danlley(danlley@126.com) Exp $
 */
public class ObjectModel {

    private long   id;
    private String content;

    public ObjectModel(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

 

 

6、编写springboot启动程序

/**
 * Danlley Wei (mailto://danlley@126.com)
 * Copyright (c) 2005-2017 All Rights Reserved.
 */
package com.myteay.restful.service.impl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author danlley(danlley@126.com)
 * @version $Id: Application.java, v 0.1 2017年4月13日 下午11:11:15 danlley(danlley@126.com) Exp $
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 

 

 

7、启动服务,用postman推送数据进行测试,地址:http://localhost:8080/greetting

{
  "id": -1193959466,
  "content": "danlley"
}

 

8、用postman测试复杂入参对象的rest服务,定义一个稍微复杂点的嵌套对象

/**
 * Danlley Wei (mailto://danlley@126.com)
 * Copyright (c) 2005-2017 All Rights Reserved.
 */
package com.myteay.restful.service.impl;

import java.io.Serializable;

/**
 * @author danlley(danlley@126.com)
 * @version $Id: ObjectModel.java, v 0.1 2017年4月13日 下午11:09:21 danlley(danlley@126.com) Exp $
 */
public class ObjectModel implements Serializable {

    /**  */
    private static final long serialVersionUID = -7268281773027468885L;
    private String            id;
    private String            content;

    public ObjectModel() {
    }

    /**
     * Setter method for property <tt>id</tt>.
     * 
     * @param id value to be assigned to property id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * Setter method for property <tt>content</tt>.
     * 
     * @param content value to be assigned to property content
     */
    public void setContent(String content) {
        this.content = content;
    }

    public ObjectModel(String id, String content) {
        this.id = id;
        this.content = content;
    }

    public String getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

 注:这里如果有其他构造方法,需要指明默认的构造方法,否则在服务端接收到传过来的json数据格式时,无法进行对象重建过程。

 

/**
 * Danlley Wei (mailto://danlley@126.com)
 * Copyright (c) 2005-2017 All Rights Reserved.
 */
package com.myteay.restful.service.impl;

import java.io.Serializable;

/**
 * 
 * @author danlley(danlley@126.com)
 * @version $Id: ApplicationModel.java, v 0.1 2017年4月16日 下午9:46:23 danlley(danlley@126.com) Exp $
 */
public class ApplicationModel implements Serializable {

    /**  */
    private static final long serialVersionUID = -9078227456354860981L;

    /**  */
    private ObjectModel       keyModel;

    /**  */
    private String key;

    /**  */
    private String value;

    /**
     * Getter method for property <tt>keyModel</tt>.
     * 
     * @return property value of keyModel
     */
    public ObjectModel getKeyModel() {
        return keyModel;
    }

    /**
     * Setter method for property <tt>keyModel</tt>.
     * 
     * @param keyModel value to be assigned to property keyModel
     */
    public void setKeyModel(ObjectModel keyModel) {
        this.keyModel = keyModel;
    }

    /**
     * Getter method for property <tt>key</tt>.
     * 
     * @return property value of key
     */
    public String getKey() {
        return key;
    }

    /**
     * Setter method for property <tt>key</tt>.
     * 
     * @param key value to be assigned to property key
     */
    public void setKey(String key) {
        this.key = key;
    }

    /**
     * Getter method for property <tt>value</tt>.
     * 
     * @return property value of value
     */
    public String getValue() {
        return value;
    }

    /**
     * Setter method for property <tt>value</tt>.
     * 
     * @param value value to be assigned to property value
     */
    public void setValue(String value) {
        this.value = value;
    }

}

 

 

9、在RestfulController类中增加一个greet服务,明确指明是post方式,同时将参数指定为ApplicationModel类型

/**
 * Danlley Wei (mailto://danlley@126.com)
 * Copyright (c) 2005-2017 All Rights Reserved.
 */
package com.myteay.restful.service.impl;

import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * @author danlley(danlley@126.com)
 * @version $Id: RestfulController.java, v 0.1 2017年4月13日 下午11:05:43 danlley(danlley@126.com) Exp $
 */
@RestController
public class RestfulController {

    /**  */
    private static final Logger logger = Logger.getLogger(RestfulController.class);

    @RequestMapping("/greetting")
    public ObjectModel greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        System.out.println("-------------------------------->" + name);
        return new ObjectModel(new Random(100).nextInt() + "", name);
    }

    @RequestMapping(value = "/greet", method = { RequestMethod.POST })
    public ApplicationModel greeting(@RequestBody ApplicationModel model,
                                     HttpServletRequest request, HttpServletResponse response) {
        logger.warn("-------------------->model=" + model);
        return model;
    }

}

 

 

 

10、设置postman的header如下:

key=Content-Type
value=application/json;charset=UTF-8

 

 

 

11、在postman中指明row格式的Body信息如下:

{
    "keyModel": {
        "id": "test",
        "content": "danlley-----------"
    },
    "key":"my",
    "value":"alipay"
}

 

 

12、通过postman发送请求,得到返回结果如下:

{
  "keyModel": {
    "id": "test",
    "content": "danlley-----------"
  },
  "key": "my",
  "value": "alipay"
}

 

 

分享到:
评论

相关推荐

    RESTful Web 服务 - 快速指南

    - **RESTful Web 服务**是基于REST架构构建的Web服务。这类服务通过HTTP方法(如GET、POST、PUT、DELETE等)来操作资源,并且通常使用JSON或XML格式来交换数据。 #### 二、REST架构关键概念 1. **资源(Resource)**...

    REST_资源指南_restful

    REST(Representational State Transfer,表述性状态转移)是一种软件架构风格,最初由Roy Fielding...对于开发者而言,无论是从学习角度,还是从应用角度,掌握RESTful架构风格都是构建现代Web服务不可或缺的一部分。

    使用Springboot系统构建RESTful Web服务.pdf

    RESTful架构以其简洁、易理解的特点成为了构建Web服务的标准模式之一。Spring Boot作为Java领域内流行的框架,极大简化了开发过程,使得开发者能够快速搭建并运行RESTful Web服务。 #### 二、核心知识点详解 #####...

    构建高效RESTful API:Golang最佳实践指南

    通过本文的指南,开发者可以构建高效、安全、可扩展的RESTful API,满足现代Web应用的需求。 本文详细介绍了使用Golang开发RESTful API的最佳实践,包括设计原则、编码实践、安全性、性能优化、监控和测试等方面。...

    Java_一个非常简单的库,用于构建生产就绪的RESTful web服务.zip

    `说明.txt`文件可能是关于如何使用此库的详细指南,包括安装步骤、创建第一个RESTful服务的示例、配置项解释以及如何运行和测试服务等内容。阅读这份文档对于理解如何利用Dropwizard构建RESTful服务至关重要。 总结...

    [云框架]基于SpringCloud的微服务架构-用户指南

    微服务架构是一种将单一应用程序分解为一组小型独立服务的方法,每个服务运行在其自己的进程中,通常与轻量级通信机制(如HTTP RESTful API)一起工作。这种架构模式有助于提高系统的可伸缩性、灵活性和容错性。 2....

    REST开发者RESTful资源指南.docx

    在RESTful架构中,资源是核心概念,通过URI(统一资源标识符)进行标识,HTTP协议中的方法(GET、POST、PUT、DELETE等)则用来操作这些资源。这种设计使RESTful服务易于理解和实现,同时降低了服务器和客户端之间的...

    Python语言开发RESTful API指南

    RESTful API提供了一种简单、高效、与语言无关的方法来构建Web服务。Python作为一种广泛使用的高级编程语言,以其简洁明了的语法、丰富的库支持和跨平台特性,在开发RESTful API方面具有明显的优势。接下来,让我们...

    RESTful PHP Web Services

    本书详细介绍了如何通过PHP实现RESTful架构风格的Web服务,并且通过大量的实例来加深理解。从基础的架构概念到实际的PHP代码实现,作者带领读者一步步地了解并掌握在PHP中创建RESTful Web Services的完整过程。这是...

    spring-cloud 构建一个restful的web服务.pdf

    REST(Representational State Transfer)是一种架构风格,常用于构建现代、可扩展的Web服务。Spring Cloud是Spring框架的一个扩展,它提供了许多微服务开发中的关键组件,如服务发现、配置中心、断路器等。 首先,...

    spring-cloud 构建一个restful的web服务.docx

    【标题】:构建 RESTful Web 服务:Spring Cloud 指南 【描述】:本文档将指导您如何使用 Spring Cloud 创建一个简单的 RESTful Web 服务。我们将通过一个"Hello World"示例来演示如何设置和运行服务,该服务能够...

    RESTful Web Services with Dropwizard.pdf

    Dropwizard是一个开源的Java框架,它用于构建支持RESTful架构的服务。这种架构允许开发者构建可互操作的Web服务,这些服务通常用于Web API的开发。RESTful API是一组由网络(通常是Web)通过HTTP协议提供服务的规则...

    RESTful Java Web Services (2009).pdf

    #### 一、RESTful架构原理与概念 - **REST(Representational State Transfer)**:一种网络应用程序的设计风格和开发方式,基于约束条件和原则,利用HTTP协议来实现客户端与服务器之间的交互。 - **核心特点**: -...

    微服务架构实战指南: 构建与治理高可用微服务系统

    ### 微服务架构实战指南:构建与治理高可用微服务系统 #### 一、系统架构演变及微服务架构概述 ##### 1.1 系统架构演变 随着互联网技术的不断发展,网站应用规模不断扩大,系统架构也随之演变。从最初的单体应用...

    RESTFUL_JAVA_WEB_SERVICES_THIRD_EDITION.pdf

    本书旨在帮助读者理解和掌握RESTful架构风格在Java web服务中的应用。 REST(Representational State Transfer)是一种软件架构风格,广泛用于构建web服务,特别是互联网应用程序。它的核心理念是通过HTTP协议中的...

    QC8.2开放式架构指南

    1. **系统设计原则**:包括模块化设计、层次化结构、以及面向服务的架构(SOA)等。 2. **标准与规范**:如TCP/IP网络协议、XML数据交换、RESTful API设计等,这些都是实现开放架构的关键元素。 3. **接口设计**:...

    基于 Java Spring 将上述类的方法对外提供 RESTful 服务并调用

    本文档提供了一个详细的指南,演示了如何使用 Java Spring 实现 RESTful 服务。Spring MVC 提供了方便的注解和工具,简化了构建 REST API 的过程。通过遵循上述步骤,开发者可以快速地将业务逻辑封装为服务,并对外...

Global site tag (gtag.js) - Google Analytics