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的更多相关文章

  1. Spring Boot (#1 quick start)

    Spring Boot (#1 quick start) 官方文档 Spring Boot是为了简化Spring应用的创建.运行.调试.部署等而出现的,使用它可以做到专注于Spring应用的开发,而无 ...

  2. spring boot使用swagger生成api接口文档

    前言 在之前的文章中,使用mybatis-plus生成了对应的包,在此基础上,我们针对项目的api接口,添加swagger配置和注解,生成swagger接口文档 具体可以查看本站spring boot ...

  3. Spring Boot实战:Restful API的构建

    上一篇文章讲解了通过Spring boot与JdbcTemplate.JPA和MyBatis的集成,实现对数据库的访问.今天主要给大家分享一下如何通过Spring boot向前端返回数据. 在现在的开 ...

  4. 基于VS Code创建Spring Boot项目开发REST API(一)

    公司从.NET转向Java不仅仅是简单的代码变成Java,趁此机会对原有的架构和代码重构,融入新的概念和技术.目前通过前后端分离,将后端更多的微服务化.从.NET转向Java我们更多的是用Java开发 ...

  5. Spring Boot Admin Quick Start

    Quick Start 1. Spring Boot Admin是干什么的? 用来监控Spring Boot项目,可以通过Spring Boot Admin Client(via Http) 或者 使 ...

  6. spring boot:接口站增加api版本号后的安全增强(spring boot 2.3.3)

    一,接口站增加api版本号后需要做安全保障? 1,如果有接口需要登录后才能访问的, 需要用spring security增加授权 2,接口站需要增加api版本号的检验,必须是系统中定义的版本号才能访问 ...

  7. spring boot: 设计接口站api的版本号,支持次版本号(spring boot 2.3.2)

    一,为什么接口站的api要使用版本号? 1,当服务端接口的功能发生改进后, 客户端如果不更新版本,    则服务端返回的功能可能不能使用,    所以在服务端功能升级后,     客户端也要相应的使用 ...

  8. 只需一步,在Spring Boot中统一Restful API返回值格式与统一处理异常

    ## 统一返回值 在前后端分离大行其道的今天,有一个统一的返回值格式不仅能使我们的接口看起来更漂亮,而且还可以使前端可以统一处理很多东西,避免很多问题的产生. 比较通用的返回值格式如下: ```jav ...

  9. Spring Boot 整合Swagger2构建API文档

    1.pom.xml中引入依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...

随机推荐

  1. JS计算两个日期之间的天数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. eclipse启动几秒后报错 (一闪而过)

    eclipse启动报错,让查看.metadata/.log日志   1 !SESSION 2013-09-23 17:28:28.484 ------------------------------- ...

  3. docker从零开始(二)容器初体验

    使用定义容器 Dockerfile Dockerfile定义容器内所需要的环境.对网络接口和磁盘驱动器等资源的访问在此环境中进行虚拟化,该环境与系统的其他部分隔离,因此您需要将端口映射到外部世界,并具 ...

  4. .net core 2.0 报错:error NU1102: Unable to find package ...

    原文地址:传送门 这种是nuget无法还原的问题.解决问题的方法: 在项目文件所在的目录下创建文件:NuGet.Config 里面内容: <?xml version="1.0" ...

  5. [转]认识session

    今天想用一个session来实现用户登录判断,也算是对之前session的探究,查了下资料session的运行机制如下: session是服务器端的一种会话机制,当客户端的请求服务器创建一个sessi ...

  6. codeforces Round #441 C Classroom Watch【枚举/注意起点】

    C. time limit per test 1 second memory limit per test 512 megabytes input standard input output stan ...

  7. POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16625   Accepted: 5383 Descri ...

  8. 差分+树状数组【p4868】Preprefix sum

    Description 前缀和(prefix sum)\(S_i=\sum_{k=1}^i a_i\). 前前缀和(preprefix sum) 则把\(S_i\)作为原序列再进行前缀和.记再次求得前 ...

  9. 【欧拉函数】BZOJ2190-[SDOI2012]longge的数学问题

    [题目大意] 求出∑gcd(i, N)(1<=i <=N). [思路] 对于x=ak,y=bk,若gcd(a,b)=1则必有gcd(x,y)=1.枚举N的所有因数,∑gcd(i, N)=∑ ...

  10. Scala实战高手****第13课:Scala模式匹配实战和Spark源码鉴赏

    package com.dt.spark.scala.bascis class Dataframework case class Computerframework (name:String,popu ...