BAEL-4842 - Use React and Spring Boot to Build a Simple CRUD App
Initial source module for the article
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.springbootreact;
|
||||
|
||||
import com.baeldung.springbootreact.domain.Client;
|
||||
import com.baeldung.springbootreact.repository.ClientRepository;
|
||||
import com.github.javafaker.Faker;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@Component
|
||||
public class BoostrapInitialData implements CommandLineRunner {
|
||||
|
||||
private final ClientRepository clientRepository;
|
||||
private final Faker faker = new Faker(Locale.getDefault());
|
||||
|
||||
public BoostrapInitialData(ClientRepository clientRepository) {
|
||||
this.clientRepository = clientRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
clientRepository.save(new Client(faker.name().fullName(), faker.internet().emailAddress()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.springbootreact;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootReactApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootReactApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.springbootreact.controller;
|
||||
|
||||
import com.baeldung.springbootreact.domain.Client;
|
||||
import com.baeldung.springbootreact.repository.ClientRepository;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/clients")
|
||||
public class ClientsController {
|
||||
|
||||
private final ClientRepository clientRepository;
|
||||
|
||||
public ClientsController(ClientRepository clientRepository) {
|
||||
this.clientRepository = clientRepository;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List getClients() {
|
||||
return clientRepository.findAll();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Client getClient(@PathVariable Long id) {
|
||||
return clientRepository.findById(id).orElseThrow(RuntimeException::new);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity createClient(@RequestBody Client client) throws URISyntaxException {
|
||||
Client savedClient = clientRepository.save(client);
|
||||
return ResponseEntity.created(new URI("/clients/" + savedClient.getId())).body(savedClient);
|
||||
}
|
||||
|
||||
@PutMapping("{id}")
|
||||
public ResponseEntity updateClient(@PathVariable Long id, @RequestBody Client client) {
|
||||
Client currentClient = clientRepository.findById(id).orElseThrow(RuntimeException::new);
|
||||
currentClient.setName(client.getName());
|
||||
currentClient.setEmail(client.getEmail());
|
||||
currentClient = clientRepository.save(client);
|
||||
|
||||
return ResponseEntity.ok(currentClient);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity deleteClient(@PathVariable Long id) {
|
||||
clientRepository.deleteById(id);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.springbootreact.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "client")
|
||||
public class Client {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
public Client() {
|
||||
}
|
||||
|
||||
public Client(String name, String email) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Client(Long id, String name, String email) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
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 getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.springbootreact.repository;
|
||||
|
||||
import com.baeldung.springbootreact.domain.Client;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface ClientRepository extends JpaRepository<Client, Long> {
|
||||
}
|
||||
Reference in New Issue
Block a user