@RestController
@RequestMapping(value="/api")
public class UserResource
extends java.lang.Object
This class accesses the User entity, and needs to fetch its collection of authorities.
For a normal use-case, it would be better to have an eager relationship between User and Authority, and send everything to the client side: there would be no View Model and DTO, a lot less code, and an outer-join which would be good for performance.
We use a View Model and a DTO for 3 reasons:
Another option would be to have a specific JPA entity graph to handle this case.
Constructor and Description |
---|
UserResource(UserRepository userRepository,
UserService userService,
MailService mailService) |
Modifier and Type | Method and Description |
---|---|
org.springframework.http.ResponseEntity<User> |
createUser(UserDTO userDTO)
POST /users : Creates a new user.
|
org.springframework.http.ResponseEntity<java.lang.Void> |
deleteUser(java.lang.String login)
DELETE /users/:login : delete the "login" User.
|
org.springframework.http.ResponseEntity<java.util.List<UserDTO>> |
getAllUsers(org.springframework.data.domain.Pageable pageable)
GET /users : get all users.
|
java.util.List<java.lang.String> |
getAuthorities() |
org.springframework.http.ResponseEntity<UserDTO> |
getUser(java.lang.String login)
GET /users/:login : get the "login" user.
|
org.springframework.http.ResponseEntity<UserDTO> |
updateUser(UserDTO userDTO)
PUT /users : Updates an existing User.
|
public UserResource(UserRepository userRepository, UserService userService, MailService mailService)
@PostMapping(value="/users") @Timed @Secured(value="ROLE_ADMIN") public org.springframework.http.ResponseEntity<User> createUser(@RequestBody UserDTO userDTO) throws java.net.URISyntaxException
Creates a new user if the login and email are not already used, and sends an mail with an activation link. The user needs to be activated on creation.
userDTO
- the user to createjava.net.URISyntaxException
- if the Location URI syntax is incorrectBadRequestAlertException
- 400 (Bad Request) if the login or email is already in use@PutMapping(value="/users") @Timed @Secured(value="ROLE_ADMIN") public org.springframework.http.ResponseEntity<UserDTO> updateUser(@RequestBody UserDTO userDTO)
userDTO
- the user to updateEmailAlreadyUsedException
- 400 (Bad Request) if the email is already in useLoginAlreadyUsedException
- 400 (Bad Request) if the login is already in use@GetMapping(value="/users") @Timed public org.springframework.http.ResponseEntity<java.util.List<UserDTO>> getAllUsers(org.springframework.data.domain.Pageable pageable)
pageable
- the pagination information@GetMapping(value="/users/authorities") @Timed @Secured(value="ROLE_ADMIN") public java.util.List<java.lang.String> getAuthorities()
@GetMapping(value="/users/{login:^[_\'.@A-Za-z0-9-]*$}") @Timed public org.springframework.http.ResponseEntity<UserDTO> getUser(@PathVariable java.lang.String login)
login
- the login of the user to find@DeleteMapping(value="/users/{login:^[_\'.@A-Za-z0-9-]*$}") @Timed @Secured(value="ROLE_ADMIN") public org.springframework.http.ResponseEntity<java.lang.Void> deleteUser(@PathVariable java.lang.String login)
login
- the login of the user to deleteCopyright © 2017 Osgiliath. All rights reserved.