开发手册 欢迎您!
软件开发者资料库

使用RESTful Web服务

Spring Boot使用RESTful Web服务 - 从基本概念到高级概念,从简单而简单的步骤学习Spring Boot,其中包括简介,快速入门,引导,Tomcat部署,构建系统,代码结构,Spring Bean和依赖注入,Runners,应用程序属性等示例,日志记录,构建RESTful Web服务,异常处理,拦截器,Servlet过滤器,Tomcat端口号,休息模板,文件处理,服务组件,Thymeleaf,使用RESTful Web服务,CORS支持,国际化,调度,启用HTTPS,Eureka服务器,服务注册Eureka,Zuul代理服务器和路由,Spring云配置服务器,Spring云配置客户端,执行器,管理服务器,管理客户端,启用Swagger2,创建Docker镜像,跟踪微服务日志,Flyway数据库,发送电子邮件,Hystrix,Web套接字,批处理服务,Apache Kafka的Spring,Twilio,单元测试用例,Rest控制器单元测试,数据库Handli ng,使用JWT保护Web应用程序,OAuth2,Google Cloud Platform,Google OAuth2登录。

本章将详细讨论如何使用jQuery AJAX来使用RESTful Web服务.

创建一个简单的Spring Boot Web应用程序并编写一个控制器类文件,用于重定向到HTML文件使用RESTful Web服务.

我们需要在构建配置文件中添加Spring Boot启动程序Thymeleaf和Web依赖项.

对于Maven用户,请在pom.xml文件中添加以下依赖项.

   org.springframework.boot   spring-boot-starter-thymeleaf   org.springframework.boot   spring-boot-starter-web

对于Gradle用户,将以下依赖项添加到build.gradle文件中 :

compile group: ‘org.springframework.boot’, name: ‘spring-boot-starter-thymeleaf’compile(‘org.springframework.boot:spring-boot-starter-web’)

@Controller类文件的代码低于 :

@Controllerpublic class ViewController {}

您可以定义请求URI方法以重定向到HTML文件,如下所示 : 去;

@RequestMapping("/view-products")public String viewProducts() {   return "view-products";}@RequestMapping("/add-products")public String addProducts() {   return "add-products";}

此API http://localhost:9090/products 应返回以下JSON作为响应,如图所示低于 :

[   {      "id": "1",      "name": "Honey"   },   {      "id": "2",      "name": "Almond"   }]

现在,在类路径的templates目录下创建一个view-products.html文件.

在HTML文件中,我们添加了jQuery库并编写了代码以在页面加载时使用RESTful Web服务.

POST方法和此URL http://localhost:9090/products 应包含以下请求正文和响应正文.

Request body的代码在下面给出 :

{   "id":"3",   "name":"Ginger"}

响应正文的代码在下面给出 :

Product is created successfully

现在,创建添加产品.类路径中templates目录下的html文件.

在HTML文件中,我们添加了jQuery库,并在单击按钮时编写了将表单提交给RESTful Web服务的代码.

完整的代码如下.

Maven  -  pom.xml文件

      4.0.0   com.IT屋   demo   0.0.1-SNAPSHOT   jar   demo   Demo project for Spring Boot         org.springframework.boot      spring-boot-starter-parent      1.5.8.RELEASE                  UTF-8      UTF-8      1.8                     org.springframework.boot         spring-boot-starter-web                     org.springframework.boot         spring-boot-starter-test         test                     org.springframework.boot         spring-boot-starter-thymeleaf                                       org.springframework.boot            spring-boot-maven-plugin                     

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.it1352’version = ‘0.0.1-SNAPSHOT’sourceCompatibility = 1.8repositories {   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’)}

c下面给出的ontroller类文件 -  ViewController.java在下面给出 :

package com.it1352.demo.controller; import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class ViewController {   @RequestMapping("/view-products")   public String viewProducts() {      return "view-products";   }   @RequestMapping("/add-products")   public String addProducts() {      return "add-products";      }   }

view-products.html文件的编号低于 :

               View Products                                  
   

add-products.html文件在下面和下面给出;

               Add Products                                    

主要的Spring Boot Application类文件在下面和下面给出;

package com.it1352.demo; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DemoApplication {   public static void main(String[] args) {      SpringApplication.run(DemoApplication.class, args);   }}

现在,您可以创建一个可执行的JAR文件,并使用以下Maven运行Spring Boot应用程序或Gradle命令.

对于Maven,使用下面给出的命令 :

mvn clean install

在"BUILD SUCCESS"之后,您可以在目标目录下找到JAR文件.

对于Gradle,请使用下面给出的命令 :

gradle clean build

之后" "BUILD SUCCESSFUL",你可以在build/libs目录下找到JAR文件.

使用以下命令运行JAR文件 :

java -jar 

现在,应用程序已在Tomcat端口8080上启动.

在Tomcat上启动应用程序Port_8080

现在点击Web浏览器中的URL,您可以看到输出显示 :

http://localhost:8080/view-products

1Honey_2Almond

http://localhost:8080/add-products

提交表单Spring Boot

现在,点击按钮点击此处提交表格,您可以看到结果显示 :

提交表单Spring Boot输出窗口

现在,点击查看产品URL并查看创建的产品.

的http://本地主机:8080/视图副产物

1Honey 2Almond 3Ginger

Angular JS

要使用Angular JS来使用API,您可以使用下面给出的示例 :

使用以下代码创建Angular JS Controller以使用GET API  -   http://localhost:9090/products :

angular.module('demo', []).controller('Hello', function($scope, $http) {   $http.get('http://localhost:9090/products').   then(function(response) {      $scope.products = response.data;   });});

使用以下代码创建Angular JS Controller以使用POST API  -   http://localhost:9090/products  :

angular.module('demo', []).controller('Hello', function($scope, $http) {   $http.post('http://localhost:9090/products',data).   then(function(response) {      console.log("Product created successfully");   });});

注意 :  Post方法数据表示JSON格式的Request主体,用于创建产品.