-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathUserController.java
More file actions
36 lines (30 loc) · 1.2 KB
/
UserController.java
File metadata and controls
36 lines (30 loc) · 1.2 KB
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
package com.integrationninjas.springbootexample.controller;
import java.util.List;
import com.integrationninjas.springbootexample.dto.UserDto;
import com.integrationninjas.springbootexample.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public ResponseEntity<List<UserDto>> getUsers() {
List<UserDto> usersList = userService.getUsers();
return new ResponseEntity<>(usersList, HttpStatus.OK);
}
@PostMapping("/users")
public ResponseEntity<String> createUser(@RequestBody UserDto userDto) {
try {
String status = userService.createUser(userDto);
return new ResponseEntity<>(status, HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}