Sobes.tech
Middle

public class Main { public static void main(String[] args) { System.out.println("Hello World"); } } -------------------------------- @SpringBootApplication @RestController @RequestMapping("/tasks") public class Main{ private final List<Task> tasks = new ArrayList<>(); private final AtomicLong idGenerator = new AtomicLong(1); public static void main(String[] args){ SpringApplication.run(Main.class, args); } @PostMapping @ResponseStatus(HttpStatus.CREATED) public Task createTask(@RequestBody CreateTaskRequest request){ Task task = new Task(idGenerator.getAndIncrement(), request.title()); tasks.add(task); return task; } @GetMapping public List<Task> getAllTasks(){ return tasks; } public record CreateTaskRequest(String title){ } public record Task(Long id, String title){ } }

sobes.tech AI

Answer from AI

The provided code implements a simple REST API on Spring Boot for task management.

  • The Main class is annotated with @SpringBootApplication, @RestController, and @RequestMapping("/tasks"), making it the entry point and controller with the base URL /tasks.
  • It contains a list of tasks tasks and a unique ID generator idGenerator.
  • The createTask method handles POST requests, creates a new task with a unique ID, and adds it to the list.
  • The getAllTasks method returns the list of all tasks on a GET request.
  • Record classes CreateTaskRequest and Task are used for convenient data storage.

This is a simple CRUD example of "create" and "get all" tasks without database connection, stored in memory.