+spring rest example
This commit is contained in:
@ -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))
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
./
|
||||
├── LoadDatabase.java
|
||||
├── RestApplication.java
|
||||
├── StudentController.java
|
||||
├── Student.java
|
||||
└── StudentRepository.java
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
37
Java/spring/rest/src/main/java/com/example/rest/Student.java
Normal file
37
Java/spring/rest/src/main/java/com/example/rest/Student.java
Normal 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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package myPackage;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
interface StudentRepository extends JpaRepository<Student, Long> {}
|
@ -0,0 +1 @@
|
||||
|
@ -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() {
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user