Skip to content

Instantly share code, notes, and snippets.

Last active April 6, 2020 18:37
What would you like to do?
public class UserController {
private UserService service;
public UserController(UserService service) {
this.service = service;
public List<User> findAll() {
return service.findAll();
public User findUser(@PathVariable long userId) {
return service.findUser(userId);
public ResponseEntity<User> createUser(@RequestBody User user) {
User createdUser = service.createUser(user);
return ResponseEntity.created(URI.create(String.format("/api/v1/users/%d", createdUser.getId())))
public User updateUser(@PathVariable long userId, @RequestBody User user) {
return service.updateUser(userId, user);
public ResponseEntity<Void> deleteUser(@PathVariable long userId) {
return ResponseEntity.ok().build();
public ResponseEntity<String> clientError(ClientException e) {
return ResponseEntity.badRequest().body(e.getMessage());
public ResponseEntity<String> resourceNotFound(NotFoundException e) {
return ResponseEntity.notFound().build();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment