I have a strange problem with MySql. I did a small project with Spring-Boot MySql but when I try to add a new User to the database it gives me the success message but then going to see on mysql shell nothing appears in the database even if there is (because with the Get it actually finds the inserted user). Now I don't understand what the problem is. I attach the applications. properties of my Spring project.
every time I ask for Get it returns this MESSAGE:
D:\>curl -G localhost:8080/demo/first -d name=Biagio
{"name":"Biagio","timestamp":"2020-10-05T08:54:18.623+00:00","email":"biagio@gmal.com","surname":"Biagio2"}
My Database:
+----+------------------------+----------+----------------------------+---------+
| id | email | name | stmp | surname |
+----+------------------------+----------+----------------------------+---------+
| 32 | mirketto90@yahoo.it | Mirko | 2020-10-01 12:31:47.827000 | NULL |
| 36 | biagio@gmail.com | Biagio | 2020-10-01 16:31:31.687000 | Vaso |
| 37 | biagio@gmail.com | Biagio | 2020-10-01 16:31:50.077000 | Vaso |
| 38 | biagio@gmail.com | Biagio | 2020-10-01 18:35:45.992000 | Vaso |
| 39 | | Mirko | 2020-10-02 13:24:05.840000 | Vaso |
| 40 | mirkoparioli@gmail.com | Mirko | 2020-10-02 13:24:23.383000 | Vaso |
| 47 | giovanni@gmal.com | Giovanni | 2020-10-05 12:11:05.997000 | John |
+----+------------------------+----------+----------------------------+---------+
I'm sure I'm using the right database (db_example), this is my
@SpringBootApplication
public class AccessingDataMysqlApplication {
public static void main(String[] args) {
SpringApplication.run(AccessingDataMysqlApplication.class, args);
}
}
@RestController@RequestMapping("/demo")
public class MainController {
@Autowired
private UserService userService;
@Transactional
//@RequestMapping(value = "/add/", method = RequestMethod.POST)
@PostMapping(path = "/demo/add")
public String addNewUser(@PathVariable("name") String name, @PathVariable("email") String email, @PathVariable("surname") String surname) {
UserDto n = new UserDto();
n.setName(name);
n.setSurname(surname);
n.setEmail(email);
userService.create(n);
return "User Saved in DB";
}
@SuppressWarnings({
"rawtypes",
"unchecked"
})
//@RequestMapping(value = "/fetchUser/{name}", method = RequestMethod.GET)
@GetMapping("/demo/first")
public ResponseEntity < UserDto > fetchUser(@PathVariable("name") String name) {
System.out.println(name);
try {
UserDto namefound = userService.findFirstByName(name);
System.out.println("Name found");
ResponseEntity < UserDto > user = new ResponseEntity < UserDto > (namefound, HttpStatus.OK);
return user;
} catch(NoResultException ne) {
System.out.println("User not found");
return new ResponseEntity("User not found with name : " + name, HttpStatus.NOT_FOUND);
}
}
}
I solved the problem myself. There was simply too much "private" when @Autowired on UserServiceImpl.The project is fair and well connected. For anything do not hesitate to write me.