|
| 1 | +package org.baeldung.web.controller; |
| 2 | + |
| 3 | +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; |
| 4 | +import static org.apache.commons.lang3.RandomStringUtils.randomNumeric; |
| 5 | + |
| 6 | +import org.baeldung.web.dto.Bar; |
| 7 | +import org.springframework.http.HttpStatus; |
| 8 | +import org.springframework.stereotype.Controller; |
| 9 | +import org.springframework.web.bind.annotation.PathVariable; |
| 10 | +import org.springframework.web.bind.annotation.RequestBody; |
| 11 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 12 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 13 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 14 | +import org.springframework.web.bind.annotation.ResponseStatus; |
| 15 | + |
| 16 | +@Controller |
| 17 | +public class BarController { |
| 18 | + |
| 19 | + public BarController() { |
| 20 | + super(); |
| 21 | + } |
| 22 | + |
| 23 | + // API - read |
| 24 | + // @PreAuthorize("#oauth2.hasScope('bar') and #oauth2.hasScope('read')") |
| 25 | + @RequestMapping(method = RequestMethod.GET, value = "/bars/{id}") |
| 26 | + @ResponseBody |
| 27 | + public Bar findById(@PathVariable final long id) { |
| 28 | + return new Bar(Long.parseLong(randomNumeric(2)), randomAlphabetic(4)); |
| 29 | + } |
| 30 | + |
| 31 | + // API - write |
| 32 | + // @PreAuthorize("#oauth2.hasScope('bar') and #oauth2.hasScope('write') and hasRole('ROLE_ADMIN')") |
| 33 | + @RequestMapping(method = RequestMethod.POST, value = "/bars") |
| 34 | + @ResponseStatus(HttpStatus.CREATED) |
| 35 | + @ResponseBody |
| 36 | + public Bar create(@RequestBody final Bar bar) { |
| 37 | + bar.setId(Long.parseLong(randomNumeric(2))); |
| 38 | + return bar; |
| 39 | + } |
| 40 | + |
| 41 | +} |
0 commit comments