[Spring Boot ] Creating the Spring Boot Project : Demo: Creating a REST Controller

时间:2024-12-11 17:37:02

In Spring boot, define a REST API endpoint is pretty easy.

package com.globomatisc.bike.controllers;

import java.util.ArrayList;
import java.util.List; import com.globomatisc.bike.models.Bike;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*; @RestController
@RequestMapping("/api/v1/bikes")
public class BikesController { @GetMapping
public List<Bike> list() {
List<Bike> bikes = new ArrayList<>();
return bikes;
} @PostMapping
@ResponseStatus(HttpStatus.OK)
public void create(@RequestBody Bike bike) { } @GetMapping("/{id}")
public Bike get(@PathVariable("id") long id) {
return new Bike();
}
}