Spring Boot 教程:消费 Rest Web 服务
发布日期:2021-05-14 00:52:27 浏览次数:11 分类:博客文章

本文共 5898 字,大约阅读时间需要 19 分钟。

【注】本文译自:
    本文将讨论如何使用 jQuery AJAX 来消费 RESTful Web 服务。
    创建一个简单的 Spring Boot web 应用并编写一个控制器类文件用于重定向到 HTML 文件中,以消费 RESTful web 服务。
    我们要在构件配置文件中加上 Spring Boot starter Thymeleaf 和 Web 依赖。
    对于 Maven 用户,在 pom.xml 文件中加上以下依赖:
1 
2
org.springframework.boot
3
spring-boot-starter-thymeleaf
4
5 6
7
org.springframework.boot
8
spring-boot-starter-web
9
    对于 Gradle 用户,在 build.gradle 中加上如下依赖:
1 compile group: ‘org.springframework.boot’, name: ‘spring-boot-starter-thymeleaf’2 compile(‘org.springframework.boot:spring-boot-starter-web’)
    @Controller 类文件如下:
1 @Controller2 public class ViewController {3 }
    可以定义请求 URI 方法来重定向到 HTML 文件中,如下所示:
1 @RequestMapping(“/view-products”)2 public String viewProducts() {3    return “view-products”;4 }5 @RequestMapping(“/add-products”)6 public String addProducts() {7    return “add-products”;8 }
   API  应当返回以下所示的 JSON 响应:
1 [ 2    { 3       "id": "1", 4       "name": "Honey" 5    }, 6    { 7       "id": "2", 8       "name": "Almond" 9    }10 ]
    现在,在 classpath 下的 templates 目录中创建 view-products.html 文件:
    在这个 HTML 文件中,我们加上了 jQuery 类,且编写代码在页面加载时消费 RESTful web 服务。
 
    POST 方法和 URL  应当包含以下请求体和响应体:
    请求体代码如下:
{   "id":"3",   "name":"Ginger"}
    响应体代码如下:
Product is created successfully
    现在,在 classpath 下的 templates 目录中创建 add-products.html 文件。
    在 HTML 文件中,我们加上 jQuery 库并编写,在单击按钮时提交表单以消费 RESTful web 服务。
1  2 
    完整的代码如下:
    Maven – pom.xml 文件:
1 
2
6 7
4.0.0
8
com.tutorialspoint
9
demo
10
0.0.1-SNAPSHOT
11
jar
12
demo
13
Demo project for Spring Boot
14 15
16
org.springframework.boot
17
spring-boot-starter-parent
18
1.5.8.RELEASE
19
20
21 22
23
UTF-8
24
UTF-8
25
1.8
26
27 28
29
30
org.springframework.boot
31
spring-boot-starter-web
32
33 34
35
org.springframework.boot
36
spring-boot-starter-test
37
test
38
39 40
41
org.springframework.boot
42
spring-boot-starter-thymeleaf
43
44
45 46
47
48
49
org.springframework.boot
50
spring-boot-maven-plugin
51
52
53
54 55
    Gradle – build.gradle 代码如下:
buildscript {   ext {      springBootVersion = ‘1.5.8.RELEASE’   }   repositories {      mavenCentral()   }   dependencies {      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")   }} apply plugin: ‘java’apply plugin: ‘eclipse’apply plugin: ‘org.springframework.boot’ group = ‘com.tutorialspoint’version = ‘0.0.1-SNAPSHOT’sourceCompatibility = 1.8 repositories {   mavenCentral()} dependencies {   compile(‘org.springframework.boot:spring-boot-starter-web’)   compile group: ‘org.springframework.boot’, name: ‘spring-boot-starter-thymeleaf’   testCompile(‘org.springframework.boot:spring-boot-starter-test’)}
   控制类代码如下:
    ViewController.java 如下:
1 package com.tutorialspoint.demo.controller; 2   3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5   6 @Controller 7 public class ViewController { 8    @RequestMapping(“/view-products”) 9    public String viewProducts() {10       return “view-products”;11    }12    @RequestMapping(“/add-products”)13    public String addProducts() {14       return “add-products”;   15    }   16 }
    view-products.html 文件如下:
1  2  3     4       
5 View Products 6 7 8 17 18 19 20
21 22 23 add-products.html 文件如下:24 25 26 27
28 Add Products29 30 31 55 56 57 58 59 60
    主 Spring Boot 应用类文件如下:
1 package com.tutorialspoint.demo; 2   3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5   6 @SpringBootApplication 7 public class DemoApplication { 8    public static void main(String[] args) { 9       SpringApplication.run(DemoApplication.class, args);10    }11 }
   现在可以使用 Maven 或 Gradle 命令创建可执行 executable JAR 文件并运行 Spring Boot 应用了:
   Maven 命令如下:
mvn clean install
   在 “BUILD SUCCESS” 之后,你可以在 target 目录下找到 JAR 文件。
   Gradle 可以使用以下命令:
gradle clean build
   在 “BUILD SUCCESSFUL” 之后,你可以在 build/libs 目录下找到 JAR 文件。
   使用以下命令运行 JAR 文件:
java –jar 
    应用已在 Tomcat 8080 端口启动,如下图所示:
    在浏览器中输入下面的 URL 你会看到如下图所示的输出:
    
    
    现在,单击按钮 Click here to submit the form 你可以看到如下图所示的结果:
    现在,单击如下查看产品 URL 查看所创建的产品。
    

Angular JS

   要使用 Angular JS 消费 APIs,如下所示:
   使用下面的代码创建 Angular JS 控制来消费 GET API - :
1 angular.module('demo', [])2 .controller('Hello', function($scope, $http) {3    $http.get('http://localhost:9090/products').4    then(function(response) {5       $scope.products = response.data;6    });7 });
   使用下面的代码创建 Angular JS 控制来消费 POST API - 
1 angular.module('demo', [])2 .controller('Hello', function($scope, $http) {3    $http.post('http://localhost:9090/products',data).4    then(function(response) {5       console.log("Product created successfully");6    });7 });
注意: Post 方法数据表示用于创建产品的 JSON 格式的请求体
 
上一篇:Spring Boot 教程:CORS 支持
下一篇:Spring Boot 教程:Thymeleaf

发表评论

最新留言

很好
[***.229.124.182]2025年04月06日 20时34分39秒