forked from jonashackt/spring-boot-vuejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackendController.java
63 lines (49 loc) · 2.37 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package de.jonashackt.springbootvuejs.controller;
import de.jonashackt.springbootvuejs.domain.User;
import de.jonashackt.springbootvuejs.exception.UserNotFoundException;
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!";
public static final String SECURED_TEXT = "Hello from the secured resource!";
@Autowired
private UserRepository userRepository;
@RequestMapping(path = "/hello")
public String sayHello() {
LOG.info("GET called on /hello resource");
return HELLO_TEXT;
}
@RequestMapping(path = "/user/{lastName}/{firstName}", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public long addNewUser (@PathVariable("lastName") String lastName, @PathVariable("firstName") String firstName) {
User savedUser = userRepository.save(new User(firstName, lastName));
LOG.info(savedUser.toString() + " successfully saved into DB");
return savedUser.getId();
}
@GetMapping(path = "/user/{id}")
public User getUserById(@PathVariable("id") long id) {
return userRepository.findById(id).map(user -> {
LOG.info("Reading user with id " + id + " from database.");
return user;
}).orElseThrow(() -> new UserNotFoundException("The user with the id " + id + " couldn't be found in the database."));
}
@RequestMapping(path="/secured", method = RequestMethod.GET)
public @ResponseBody String getSecured() {
LOG.info("GET successfully called on /secured resource");
return SECURED_TEXT;
}
// Forwards all routes to FrontEnd except: '/', '/index.html', '/api', '/api/**'
// Required because of 'mode: history' usage in frontend routing, see README for further details
@RequestMapping(value = "{_:^(?!index\\.html|api).*$}")
public String redirectApi() {
LOG.info("URL entered directly into the Browser, so we need to redirect...");
return "forward:/";
}
}