Building a web application with Spring Boot and Angular (#6396)
* Initial Commit * Update pom.xml
This commit is contained in:
committed by
KevinGilmore
parent
9cee994496
commit
ad4ff82fa2
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.application;
|
||||
|
||||
import com.baeldung.application.entities.User;
|
||||
import com.baeldung.application.repositories.UserRepository;
|
||||
import java.util.stream.Stream;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
CommandLineRunner init(UserRepository userRepository) {
|
||||
return args -> {
|
||||
Stream.of("John", "Julie", "Jennifer", "Helen", "Rachel").forEach(name -> {
|
||||
User user = new User(name, name.toLowerCase() + "@domain.com");
|
||||
userRepository.save(user);
|
||||
});
|
||||
userRepository.findAll().forEach(System.out::println);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.application.controllers;
|
||||
|
||||
import com.application.entities.User;
|
||||
import com.baeldung.application.repositories.UserRepository;
|
||||
import java.util.List;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@CrossOrigin(origins = "http://localhost:4200")
|
||||
public class UserController {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public UserController(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
@GetMapping("/users")
|
||||
public List<User> getUsers() {
|
||||
return (List<User>) userRepository.findAll();
|
||||
}
|
||||
|
||||
@PostMapping("/users")
|
||||
void addUser(@RequestBody User user) {
|
||||
userRepository.save(user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.application.entities;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
private final String name;
|
||||
private final String email;
|
||||
|
||||
public User() {
|
||||
this.name = "";
|
||||
this.email = "";
|
||||
}
|
||||
|
||||
public User(String name, String email) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "id=" + id + ", name=" + name + ", email=" + email + '}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.application.repositories;
|
||||
|
||||
import com.baeldung.application.entities.User;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
|
||||
@Repository
|
||||
@CrossOrigin(origins = "http://localhost:4200")
|
||||
public interface UserRepository extends CrudRepository<User, Long>{}
|
||||
Reference in New Issue
Block a user