|
1 | 1 | package nu.westlin.kartrepo |
2 | 2 |
|
3 | 3 | import org.springframework.beans.factory.annotation.Autowired |
| 4 | +import org.springframework.dao.IncorrectResultSizeDataAccessException |
4 | 5 | import org.springframework.http.HttpStatus |
| 6 | +import org.springframework.jdbc.core.JdbcOperations |
5 | 7 | import org.springframework.web.bind.annotation.* |
6 | | -import javax.servlet.http.HttpServletResponse |
7 | 8 |
|
8 | 9 | @RestController |
9 | | -class KartController @Autowired constructor(val kartRepository: KartRepository) { |
| 10 | +open class KartController @Autowired constructor(val kartRepository: KartRepository, val jdbcOperations: JdbcOperations) { |
| 11 | + |
| 12 | + init { |
| 13 | + println("jdbcOperations = ${jdbcOperations}") |
| 14 | + println("kartRepository = ${kartRepository}") |
| 15 | + println("kartRepository.jdbcOperations = ${kartRepository.jdbcOperations}") |
| 16 | + // Jag får inte jdbcOperations att sättas automatiskt :( |
| 17 | + kartRepository.jdbcOperations = jdbcOperations |
| 18 | + println("kartRepository.jdbcOperations = ${kartRepository.jdbcOperations}") |
| 19 | + } |
10 | 20 |
|
11 | 21 |
|
12 | 22 | @RequestMapping("/user") |
13 | | - fun greeting(@RequestParam(value = "username", defaultValue = "pwestlin") username: String, response: HttpServletResponse): User { |
14 | | - return kartRepository.load(username) ?: throw NotFoundException("User $username not found") |
| 23 | + fun greeting(@RequestParam(value = "username", defaultValue = "pwestlin") username: String): Driver { |
| 24 | + return kartRepository.load(username) |
15 | 25 | } |
16 | 26 |
|
17 | 27 | @RequestMapping("/users") |
18 | | - fun greeting(): List<User> { |
19 | | - return listOf(User("pwestlin", "Peter", "Westlin")) |
| 28 | + fun greeting(): List<Driver> { |
| 29 | + return listOf(Driver("pwestlin", "Peter", "Westlin")) |
| 30 | + } |
| 31 | + |
| 32 | + @ExceptionHandler(NotFoundException::class) |
| 33 | + @ResponseBody |
| 34 | + @ResponseStatus(value = HttpStatus.NOT_FOUND) |
| 35 | + fun notFoundException(e: NotFoundException): ErrorResource { |
| 36 | + return ErrorResource(HttpStatus.NOT_FOUND.value(), "Could not find resource") |
20 | 37 | } |
21 | 38 |
|
22 | | - @ExceptionHandler(Exception::class) |
| 39 | + @ExceptionHandler(IncorrectResultSizeDataAccessException::class) |
23 | 40 | @ResponseBody |
24 | 41 | @ResponseStatus(value = HttpStatus.NOT_FOUND) |
25 | | - fun handleException(e: NotFoundException, response: HttpServletResponse): ErrorResource { |
| 42 | + fun incorrectResultSizeDataAccessException(e: IncorrectResultSizeDataAccessException): ErrorResource { |
26 | 43 | return ErrorResource(HttpStatus.NOT_FOUND.value(), "Could not find resource") |
27 | 44 | } |
28 | 45 | } |
|
0 commit comments