Guest User

Untitled

a guest
Dec 21st, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. @Entity // This tells Hibernate to make a table out of this class
  2. public class User {
  3. @Id
  4. @GeneratedValue(strategy=GenerationType.AUTO)
  5. private Integer id;
  6.  
  7.  
  8. private String name;
  9.  
  10. private String email;
  11.  
  12. private String username;
  13.  
  14. private String password;
  15.  
  16. public Integer getId() {
  17. return id;
  18. }
  19.  
  20. public void setId(Integer id) {
  21. this.id = id;
  22. }
  23.  
  24. public String getName() {
  25. return name;
  26. }
  27.  
  28. public void setName(String name) throws DataFormatException {
  29. if(name.equals(""))
  30. {
  31. throw new DataFormatException("Mpla Mpla");
  32. }
  33. this.name = name;
  34. }
  35.  
  36. public String getEmail() {
  37. return email;
  38. }
  39.  
  40. public void setEmail(String email) {
  41.  
  42. this.email = email;
  43. }
  44.  
  45. public String getUsername() {
  46. return username;
  47. }
  48.  
  49. public void setUsername(String username) {
  50. this.username = username;
  51. }
  52.  
  53. public String getPassword() {
  54. return password;
  55. }
  56.  
  57. public void setPassword(String password) {
  58. this.password = password;
  59. }
  60.  
  61. @JsonCreator
  62. public User(@JsonProperty("name") String name, @JsonProperty("email") String email,@JsonProperty("username") String username,@JsonProperty("password") String password) throws DataFormatException {
  63. setName(name);
  64. this.email = email;
  65. this.username = username;
  66. this.password = password;
  67. }
  68. }
  69.  
  70. public class MainController {
  71. @Autowired // This means to get the bean called userRepository
  72. // Which is auto-generated by Spring, we will use it to handle the data
  73. private UserRepository userRepository;
  74. }
  75. @PostMapping(path="/add") // Map ONLY POST Requests
  76. public @ResponseBody String addNewUser (@RequestBody @Valid User user1) {
  77. // @ResponseBody means the returned String is the response, not a view name
  78.  
  79. userRepository.save(user1);
  80. return "Saved";
  81. }
  82.  
  83. @GetMapping(path="/all")
  84. public @ResponseBody Iterable<User> getAllUsers() {
  85. // This returns a JSON or XML with the users
  86. return userRepository.findAll();
  87. }
  88.  
  89. "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
  90. "message": "JSON parse error: Can not construct instance of hello.Users.User, problem: Mpla Mpla; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of hello.Users.User, problem: Mpla Mplan at [Source: java.io.PushbackInputStream@7604dc21; line: 6, column: 1]",
Add Comment
Please, Sign In to add comment