Spring Boot提供了企业级构建RESTful的webService应用

Maven添加依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>

整个porn.xml如下:

<?xml version = "1.0" encoding = "UTF-8"?>
<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.tutorialspoint</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>demo</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.8.RELEASE</version><relativePath/> </parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><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></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

Rest Mapping

@RequestMapping注解定义了请求的URL去访问REST端。定义消费者好生产者对象。这个注解默认的方法为GET

@RequestMapping(value = "/products")
public ResponseEntity<Object> getProducts() { }

Request Body

@RequestBody注解用于定义请求体的类型

public ResponseEntity<Object> createProduct(@RequestBody Product product) {
}

Path Variable

@PathVariable注解提定义了定制化的或动态的URL请求。

public ResponseEntity<Object> updateProduct(@PathVariable("id") String id) {
}

Request Parameter

@Request Parameter注解回去读取request url中的参数。其中value是必须的。

public ResponseEntity<Object> getProduct(@RequestParam(value = "name", required = false, defaultValue = "honey") String name) {
}

Get API

HTTP默认的请求就是GET,这个方法不需要HTTP的body。可以通过改变参数和路径发送不同的URL。

下面是一个简单的HTTP GET请求方法。使用HashMap存储生产者。这里使用POJO类进行存储。

下面的URL中包含/products并且将返回商品列表。下面的controller类包含了REST端的GET方法。

package com.tutorialspoint.demo.controller;import java.util.HashMap;
import java.util.Map;import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.tutorialspoint.demo.model.Product;@RestController
public class ProductServiceController {private static Map<String, Product> productRepo = new HashMap<>();static {Product honey = new Product();honey.setId("1");honey.setName("Honey");productRepo.put(honey.getId(), honey);Product almond = new Product();almond.setId("2");almond.setName("Almond");productRepo.put(almond.getId(), almond);}@RequestMapping(value = "/products")public ResponseEntity<Object> getProduct() {return new ResponseEntity<>(productRepo.values(), HttpStatus.OK);}
}

POST API

HTTP的POST方法用于创建资源。这个方法包含了请求的body。可以发送不同的url或者变量。

package com.tutorialspoint.demo.controller;import java.util.HashMap;
import java.util.Map;import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RestController;import com.tutorialspoint.demo.model.Product;@RestController
public class ProductServiceController {private static Map<String, Product> productRepo = new HashMap<>();@RequestMapping(value = "/products", method = RequestMethod.POST)public ResponseEntity<Object> createProduct(@RequestBody Product product) {productRepo.put(product.getId(), product);return new ResponseEntity<>("Product is created successfully", HttpStatus.CREATED);}
}

PUT API

更新一个存在的资源。此方法包含了Body。

URL请求/products{id},将ID更新到HashMap的Repository中。代码如下:

package com.tutorialspoint.demo.controller;import java.util.HashMap;
import java.util.Map;import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;
import com.tutorialspoint.demo.model.Product;@RestController
public class ProductServiceController {private static Map<String, Product> productRepo = new HashMap<>();@RequestMapping(value = "/products/{id}", method = RequestMethod.PUT)public ResponseEntity<Object> updateProduct(@PathVariable("id") String id, @RequestBody Product product) { productRepo.remove(id);product.setId(id);productRepo.put(id, product);return new ResponseEntity<>("Product is updated successsfully", HttpStatus.OK);}
}

DELETE API

删除存在的资源。可以添加请求Body。

package com.tutorialspoint.demo.controller;import java.util.HashMap;
import java.util.Map;import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;import com.tutorialspoint.demo.model.Product;@RestController
public class ProductServiceController {private static Map<String, Product> productRepo = new HashMap<>();@RequestMapping(value = "/products/{id}", method = RequestMethod.DELETE)public ResponseEntity<Object> delete(@PathVariable("id") String id) { productRepo.remove(id);return new ResponseEntity<>("Product is deleted successsfully", HttpStatus.OK);}
}

下面是Spring Boot启动类

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

下面是POJO类

package com.tutorialspoint.demo.model;public class Product {private String id;private String name;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

下面是ProductServiceController.java

package com.tutorialspoint.demo.controller;import java.util.HashMap;
import java.util.Map;import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;import com.tutorialspoint.demo.model.Product;@RestController
public class ProductServiceController {private static Map<String, Product> productRepo = new HashMap<>();static {Product honey = new Product();honey.setId("1");honey.setName("Honey");productRepo.put(honey.getId(), honey);Product almond = new Product();almond.setId("2");almond.setName("Almond");productRepo.put(almond.getId(), almond);}@RequestMapping(value = "/products/{id}", method = RequestMethod.DELETE)public ResponseEntity<Object> delete(@PathVariable("id") String id) { productRepo.remove(id);return new ResponseEntity<>("Product is deleted successsfully", HttpStatus.OK);}@RequestMapping(value = "/products/{id}", method = RequestMethod.PUT)public ResponseEntity<Object> updateProduct(@PathVariable("id") String id, @RequestBody Product product) { productRepo.remove(id);product.setId(id);productRepo.put(id, product);return new ResponseEntity<>("Product is updated successsfully", HttpStatus.OK);}@RequestMapping(value = "/products", method = RequestMethod.POST)public ResponseEntity<Object> createProduct(@RequestBody Product product) {productRepo.put(product.getId(), product);return new ResponseEntity<>("Product is created successfully", HttpStatus.CREATED);}@RequestMapping(value = "/products")public ResponseEntity<Object> getProduct() {return new ResponseEntity<>(productRepo.values(), HttpStatus.OK);}
}

Java笔记-构造RESTful的WebService相关推荐

  1. Java笔记-使用CXF开发WebService服务器

    这里使用CXF开发WebService,要引入下面这个Maven <dependency><groupId>org.apache.cxf</groupId>< ...

  2. springmvc学习笔记(19)-RESTful支持

    springmvc学习笔记(19)-RESTful支持 标签: springmvc springmvc学习笔记19-RESTful支持 概念 REST的样例 controller REST方法的前端控 ...

  3. Java笔记04-核心类库

    Java笔记04-核心类库 Object类 1.1 常用的包 java.lang包 -该包是Java语言中的核心包,该包中的内容由Java虚拟机自动导入 如:String类,System类等java. ...

  4. Java笔记03-Constructor Override

    Java笔记03-Constructor & Override 构造方法基本概念 构造方法是类中的一种特殊方法 它是在类创建对象(实例化)的时候自动调用的方法 这个和python中的__ini ...

  5. 数据结构与算法-java笔记一 更新中

    数据结构与算法-java笔记一 更新中 数据结构与算法 什么是数据结构.算法 数据结构学了有什么用: 线性结构 数组 特点 应用 链表 存储结构 链表类型 单链表 双向链表 双向循环链表 链表与数组的 ...

  6. 【Java笔记+踩坑】SpringBoot基础3——开发。热部署+配置高级+整合NoSQL/缓存/任务/邮件/监控

      导航: [黑马Java笔记+踩坑汇总]JavaSE+JavaWeb+SSM+SpringBoot+瑞吉外卖+SpringCloud/SpringCloudAlibaba+黑马旅游+谷粒商城 目录 ...

  7. Java笔记:抽象类和接口

    Java笔记:抽象类和接口 抽象类 接口 接口多重实现 接口继承接口 嵌套接口 工厂模式 抽象类 在普通类中,一个类必须实现自身写的所有方法,每个方法必须含有自己的方法体.即便先创建一个父类,再由后续 ...

  8. 【Java笔记+踩坑】SpringBoot——基础

      导航: [黑马Java笔记+踩坑汇总]JavaSE+JavaWeb+SSM+SpringBoot+瑞吉外卖+SpringCloud/SpringCloudAlibaba+黑马旅游+谷粒商城 目录 ...

  9. java笔记1/3 (B站hsp学java)

    JAVA基础 文章目录 JAVA基础 变量 整形(INT) 浮点型(float/double) 字符型(char) 布尔类型(boolean) 基础数据类型的转换 自动类型转换 强制类型转换 重载 可 ...

最新文章

  1. 重庆社区计算机考试题库,2020重庆社区工作者考试题库:模拟题100题(64)
  2. 页面怎么创建一个数组_怎么创建一个企业网站?
  3. Safari 不能播放Video ,Chrome等可以 问题解决。
  4. python画直方图代码-Python绘制直方图及子图的方法分析(代码示例)
  5. 使用gitkraken来push的流程
  6. 构建iscsi网络存储服务
  7. android虚线边框_Android实现代码画虚线边框背景效果
  8. 31 | 深度和广度优先搜索:如何找出社交网络中的三度好友关系?
  9. 解析 Linux 中的 VFS 文件系统机制
  10. (数据科学学习手札03)Python与R在随机数生成上的异同
  11. 完全二叉树子节点个数
  12. swagger中参数为数组dataType的设置
  13. thinkPHP 表单自动验证功能
  14. 116 Python GIL全局解释器锁
  15. 内外网切换BAT脚本
  16. 微信小程序表格前后台分页
  17. 【leetcode-652】寻找重复的子树
  18. hexo(sakura)仿gitee添加文章贡献度日历图(echarts)
  19. WiFi万能钥匙+小米手机拿到coffee店WiFi密码
  20. PAT (Basic Level) Practice (中文)1072 开学寄语(C语言)

热门文章

  1. Hibernate的多表查询,分装到一个新的实体类中的一个方法
  2. linux ls -l 详解
  3. 【编程导航】国外大神总结的实用代码,30 秒学会!
  4. “扎金花FANS”进行了改进
  5. 使用Vue2.x高效还原美团外卖项目
  6. 服务器被黑 追寻ip_我的服务器被打死,源IP暴露怎么办补救
  7. 苹果开发者用计算机语言,苹果的编程语言 Swift 是用什么开发的?
  8. python格式化输出区别_python格式化输出的区别
  9. android 渠道方案,Android多渠道打包时获取当前渠道的方法
  10. mysql导出表结构_mysql导入导出表结构及表数据及执行sql文件