[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 ...
随机推荐
- Linux添加用户并赋予/取消管理员权限
Ubuntu sudo adduser username # 添加用户 sudo adduser username sudo # 追加管理员权限 grep -Po '^sudo.+:\K.*$' /e ...
- python模块之copy
提供浅拷贝和深拷贝两种模式. =>copy(x):返回x的浅拷贝 =>deepcopy(x):返回x的深拷贝 浅拷贝和深拷贝: 浅拷贝复制不变对象,引用可变对象(如列表和字典): 深拷贝复 ...
- [ 总结 ] RHEL6/Centos6 使用OpenLDAP集中管理用户帐号
使用轻量级目录访问协议(LDAP)构建集中的身份验证系统可以减少管理成本,增强安全性,避免数据复制的问题,并提供数据的一致性.
- 【SQL】约束与触发器2
3.修改约束 3.1给约束命名 按如下格式命名: name ) CONSTRAINT NameIsKey PRIMARY KEY gender ) CONSTRAINT NoAndro CHECK ( ...
- Selenium2+python自动化67-用例失败自动截图【转载】
前言: 装饰器其实就是一个以函数作为参数并返回一个替换函数的可执行函数 上一篇讲到用装饰器解决异常后自动截图,不过并没有与unittest结合,这篇把截图的装饰器改良了下,可以实现用例执行失败自动截图 ...
- seneca的一段代码(原创)
var seneca=require('seneca')() seneca.add({cmd:'wordcount'},function(msg,respond){ var length=0; if( ...
- times、 time、clock函数说明
sysconf( _SC_CLK_TCK ) 功能 获取系统的 时钟滴答的频率. clock_gettime() clock()返回的是各个线程运行cpu时间的和, 返回值一直都是0. 定义函数: ...
- Centos 7 设置 DNS
1:nmcli con mod eno1 ipv4.dns "114.114.114.114 8.8.8.8" eno1:为网卡名 2:nmcli con up eno1 激活网卡
- Python 进阶 之 enumerate()函数
enumerate()是Python的内置函数,无需依赖包,enumerate()作用是可以将生成器包装成生成器,类似于range,但enumerate()可以自动生成索引. enumerate(pa ...
- HDU 1043 Eight 【经典八数码输出路径/BFS/A*/康托展开】
本题有写法好几个写法,但主要思路是BFS: No.1 采用双向宽搜,分别从起始态和结束态进行宽搜,暴力判重.如果只进行单向会超时. No.2 采用hash进行判重,宽搜采用单向就可以AC. No.3 ...