Affichage de données et get
Bonjour,
Je suis un tutoriel spring https://www.baeldung.com/spring-boot-angular-web, et j'en suis au moment de tester l'application.
2 problèmes : - premièrement quand je lance l'application mon system out println ne me sort pas les users mais ceci :
Code:
1 2 3 4 5
| appli.User@424ec990
appli.User@84becbe
appli.User@13dc383b
appli.User@6885f3f7
appli.User@392ef4ff |
- et le plus gros problème, je n'arrive pas à acceder aux données à l'url http://localhost:8080/users qui fait un get et me retourne cette erreur :
Code:
1 2 3 4 5 6
|
java.lang.NullPointerException: null
at appli.UserController.lambda$0(UserController.java:25) ~[classes/:na]
at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:na]
at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658) ~[na:na]
at appli.UserController.getUsers(UserController.java:23) ~[classes/:na]... |
Voici mon code :
User.java :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package appli;
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 String name = "";
private String email = "";
public User() {}
public User(String name2, String string) {
this.name = name2;
this.email = string;
}
public String getEmail() {
return email;
}
public String getName() {
return name;
}
// standard constructors / setters / getters / toString
} |
UserRepository.java :
Code:
1 2 3 4 5 6 7
| package appli;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends CrudRepository<User, Long>{} |
UserController.java :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package appli;
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 {
// standard constructors
private UserRepository userRepository;
@GetMapping("/users")
public List<User> getUsers() {
return (List<User>) userRepository.findAll();
}
@PostMapping("/users")
void addUser(@RequestBody User user) {
userRepository.save(user);
}
} |
Application.java :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package appli;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.SpringApplication;
import java.util.stream.Stream;
import org.springframework.boot.CommandLineRunner;
@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);
};
}
} |
Cordialement