[Spring boot] A quick REST API Guide
Controller:
Code below shows a basic Controller to handle GET, POST; DELETE, PUT requests.
package hello.controller; import hello.model.Shipwreck;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable; import java.util.List; @RestController
@RequestMapping("api/v1/")
public class ShipController {
@RequestMapping(value="shipwrecks", method= RequestMethod.GET)
public List <Shipwreck>list() {
return ShipwreckStub.list();
} @RequestMapping(value = "shipwrecks", method = RequestMethod.POST)
public Shipwreck create(@RequestBody Shipwreck shipwreck) {
return ShipwreckStub.create(shipwreck);
} @RequestMapping(value="shipwrecks/{id}", method = RequestMethod.GET)
public Shipwreck get(@PathVariable long id) {
return ShipwreckStub.get(id);
} @RequestMapping(value="shipwrecks/{id}", method = RequestMethod.PUT)
public Shipwreck update(@PathVariable long id, @RequestBody Shipwreck shipwreck) {
return ShipwreckStub.update(id, shipwreck);
} @RequestMapping(value="shipwrecks/{id}", method = RequestMethod.DELETE)
public Shipwreck delete(@PathVariable long id) {
return ShipwreckStub.delete(id);
}
}
Model:
package hello.model;
public class Shipwreck {
Long id;
String name;
String description;
String condition;
Integer depth;
Double latitude;
Double longitude;
Integer yearDiscovered;
public Shipwreck() { }
public Shipwreck(Long id, String name, String description, String condition, Integer depth, Double latitude, Double longitude, Integer yearDiscovered) {
this.id = id;
this.name = name;
this.description = description;
this.condition = condition;
this.depth = depth;
this.latitude = latitude;
this.longitude = longitude;
this.yearDiscovered = yearDiscovered;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
public Integer getDepth() {
return depth;
}
public void setDepth(Integer depth) {
this.depth = depth;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Integer getYearDiscovered() {
return yearDiscovered;
}
public void setYearDiscovered(Integer yearDiscovered) {
this.yearDiscovered = yearDiscovered;
}
}
Im memory data:
This is just a mock data.
package hello.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import hello.model.Shipwreck;
public class ShipwreckStub {
private static Map<Long, Shipwreck> wrecks = new HashMap<Long, Shipwreck>();
private static Long idIndex = 3L; //populate initial wrecks
static {
Shipwreck a = new Shipwreck(1L, "U869", "A very deep German UBoat", "FAIR", 200, 44.12, 138.44, 1994);
wrecks.put(1L, a);
Shipwreck b = new Shipwreck(2L, "Thistlegorm", "British merchant boat in the Red Sea", "GOOD", 80, 44.12, 138.44, 1994);
wrecks.put(2L, b);
Shipwreck c = new Shipwreck(3L, "S.S. Yongala", "A luxury passenger ship wrecked on the great barrier reef", "FAIR", 50, 44.12, 138.44, 1994);
wrecks.put(3L, c);
} public static List<Shipwreck> list() {
return new ArrayList<Shipwreck>(wrecks.values());
} public static Shipwreck create(Shipwreck wreck) {
idIndex += idIndex;
wreck.setId(idIndex);
wrecks.put(idIndex, wreck);
return wreck;
} public static Shipwreck get(Long id) {
return wrecks.get(id);
} public static Shipwreck update(Long id, Shipwreck wreck) {
wrecks.put(id, wreck);
return wreck;
} public static Shipwreck delete(Long id) {
return wrecks.remove(id);
}
}
[Spring boot] A quick REST API Guide的更多相关文章
- Spring Boot (#1 quick start)
Spring Boot (#1 quick start) 官方文档 Spring Boot是为了简化Spring应用的创建.运行.调试.部署等而出现的,使用它可以做到专注于Spring应用的开发,而无 ...
- spring boot使用swagger生成api接口文档
前言 在之前的文章中,使用mybatis-plus生成了对应的包,在此基础上,我们针对项目的api接口,添加swagger配置和注解,生成swagger接口文档 具体可以查看本站spring boot ...
- Spring Boot实战:Restful API的构建
上一篇文章讲解了通过Spring boot与JdbcTemplate.JPA和MyBatis的集成,实现对数据库的访问.今天主要给大家分享一下如何通过Spring boot向前端返回数据. 在现在的开 ...
- 基于VS Code创建Spring Boot项目开发REST API(一)
公司从.NET转向Java不仅仅是简单的代码变成Java,趁此机会对原有的架构和代码重构,融入新的概念和技术.目前通过前后端分离,将后端更多的微服务化.从.NET转向Java我们更多的是用Java开发 ...
- Spring Boot Admin Quick Start
Quick Start 1. Spring Boot Admin是干什么的? 用来监控Spring Boot项目,可以通过Spring Boot Admin Client(via Http) 或者 使 ...
- spring boot:接口站增加api版本号后的安全增强(spring boot 2.3.3)
一,接口站增加api版本号后需要做安全保障? 1,如果有接口需要登录后才能访问的, 需要用spring security增加授权 2,接口站需要增加api版本号的检验,必须是系统中定义的版本号才能访问 ...
- spring boot: 设计接口站api的版本号,支持次版本号(spring boot 2.3.2)
一,为什么接口站的api要使用版本号? 1,当服务端接口的功能发生改进后, 客户端如果不更新版本, 则服务端返回的功能可能不能使用, 所以在服务端功能升级后, 客户端也要相应的使用 ...
- 只需一步,在Spring Boot中统一Restful API返回值格式与统一处理异常
## 统一返回值 在前后端分离大行其道的今天,有一个统一的返回值格式不仅能使我们的接口看起来更漂亮,而且还可以使前端可以统一处理很多东西,避免很多问题的产生. 比较通用的返回值格式如下: ```jav ...
- Spring Boot 整合Swagger2构建API文档
1.pom.xml中引入依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...
随机推荐
- Oracle 10g 安装环境配置脚本
#!/bin/bash #Test in RHEL 5.5 for 10g c=`cat /etc/shadow | grep oracle | wc -l`if [ $c != 0 ]then w ...
- [ python ] 使用sys模块实现进度条
在写网络IO传输的时候, 有时候需要进度条来显示当前传输进度,使用 sys 模块就可以实现: sys.stdout.write() 这个函数在在控制台输出字符串不会带任何结尾,这就意味着这个输出还没有 ...
- 只有5行代码的算法——Floyd算法
Floyd算法用于求一个带权有向图(Wighted Directed Graph)的任意两点距离的算法,运用了动态规划的思想,算法的时间复杂度为O(n^3).具体方法是:设点i到点j的距离为d[i][ ...
- Zookeeper概念学习系列之zookeeper实现分布式进程监控
不多说,直接上干货! 假设要监控多台服务器上的A程序运行状态, 当发现有服务器上的A程序下线的时候, 给管理员发短信, 并且尝试重启A程序. zookeeper实现分布式进程监控主要利用zk的临时节点 ...
- Asp.Net MVC在过滤器中使用模型绑定
废话不多话,直接上代码 1.创建MVC项目,新建一个过滤器类以及使用到的实体类: public class DemoFiltersAttribute : AuthorizeAttribute { pu ...
- (1)Python 安装使用IDLE
安装 官网 https://www.python.org/ Windows x86 web-based installer 在线安装 Windows x86 executable installer ...
- Python与数据结构[3] -> 树/Tree[0] -> 二叉树及遍历二叉树的 Python 实现
二叉树 / Binary Tree 二叉树是树结构的一种,但二叉树的每一个节点都最多只能有两个子节点. Binary Tree: 00 |_____ | | 00 00 |__ |__ | | | | ...
- hdu5558
hdu5558 题意 给出一个字符串,按照特殊规则进行加密. 假设已经加密了前 \(i\) 个字符,从第 \(i+1\) 个字符开始找到 \(S[i..N]\) 的长度为 \(K\) 的最长前缀等于 ...
- ubuntu 16.04.1 LTS 初始化
gcc环境------------------sudo apt-get update && \sudo apt-get install build-essential software ...
- websocket、文件上传
支持情况: 浏览器实现了websocket的浏览器:Chrome Supported in version 4+ Firefox Supported in version 4+ Internet Ex ...