forked from jonashackt/spring-boot-vuejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackendController.java
45 lines (34 loc) · 1.45 KB
/
BackendController.java
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
34
35
36
37
38
39
40
41
42
43
44
45
package de.jonashackt.springbootvuejs.controller;
import de.jonashackt.springbootvuejs.domain.User;
import de.jonashackt.springbootvuejs.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
@RestController()
@RequestMapping("/api")
public class BackendController {
private static final Logger LOG = LoggerFactory.getLogger(BackendController.class);
public static final String HELLO_TEXT = "Hello from Spring Boot Backend!";
@Autowired
private UserRepository userRepository;
@RequestMapping(path = "/hello")
public @ResponseBody String sayHello() {
LOG.info("GET called on /hello resource");
return HELLO_TEXT;
}
@RequestMapping(path = "/user", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody long addNewUser (@RequestParam String firstName, @RequestParam String lastName) {
User user = new User(firstName, lastName);
userRepository.save(user);
LOG.info(user.toString() + " successfully saved into DB");
return user.getId();
}
@GetMapping(path="/user/{id}")
public @ResponseBody User getUserById(@PathVariable("id") long id) {
LOG.info("Reading user with id " + id + " from database.");
return userRepository.findById(id).get();
}
}