+spring rest example

This commit is contained in:
anon
2024-12-10 20:32:47 +01:00
parent b8482912c7
commit 0f9d9e1b4a
10 changed files with 189 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package myPackage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.javafaker.Faker;
@Configuration
class LoadDatabase {
private static final Logger log = LoggerFactory.getLogger(LoadDatabase.class);
@Bean
CommandLineRunner initDatabase(StudentRepository repository) {
return args -> {
Faker faker = new Faker();
for (int i = 0; i < 100; i++) {
log.info("Preloading " + repository.save(
new Student(
faker.name().firstName(),
faker.number().numberBetween(18, 22),
faker.number().numberBetween(0, 300))
)
);
}
};
}
}

View File

@ -0,0 +1,6 @@
./
├── LoadDatabase.java
├── RestApplication.java
├── StudentController.java
├── Student.java
└── StudentRepository.java

View File

@ -0,0 +1,13 @@
package myPackage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestApplication {
public static void main(String[] args) {
SpringApplication.run(RestApplication.class, args);
}
}

View File

@ -0,0 +1,37 @@
package myPackage;
import java.util.Objects;
import jakarta.persistence.*;
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
public int age;
public String name;
public int credit;
public void setId(Long id){
this.id = id;
}
public Long getId(){
return this.id;
}
Student() {}
Student(String n, int a, int c) {
this.age = a;
this.credit = c;
this.name = n;
}
@Override
public String toString() {
return String.format("(Student){%s, %d, %d}", this.name, this.age, this.credit);
}
}

View File

@ -0,0 +1,52 @@
package myPackage;
import java.util.List;
import org.springframework.web.bind.annotation.*;
@RestController
class StudentController {
private final StudentRepository repository;
StudentController(StudentRepository repository) {
this.repository = repository;
}
@GetMapping("/students")
List<Student> all() {
return repository.findAll();
}
@GetMapping("/students/{id}")
Student one(@PathVariable Long id) {
return repository.findById(id).orElse(null);
}
@PostMapping("/students")
Student newStudent(@RequestBody Student newStudent) {
return repository.save(newStudent);
}
@PutMapping("/students/{id}")
Student replaceStudent(@RequestBody Student newStudent, @PathVariable Long id) {
return repository.findById(id)
.map(student -> {
student.name = newStudent.name;
student.age = newStudent.age;
student.credit = newStudent.credit;
return repository.save(student);
})
.orElseGet(() -> {
newStudent.setId(id);
return repository.save(newStudent);
});
}
@DeleteMapping("/students/{id}")
void deleteStudent(@PathVariable Long id) {
repository.deleteById(id);
}
}

View File

@ -0,0 +1,5 @@
package myPackage;
import org.springframework.data.jpa.repository.JpaRepository;
interface StudentRepository extends JpaRepository<Student, Long> {}

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,13 @@
package com.example.rest;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class RestApplicationTests {
@Test
void contextLoads() {
}
}