forked from Realhedin/topjava02
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminRestController.java
More file actions
59 lines (49 loc) · 2.2 KB
/
AdminRestController.java
File metadata and controls
59 lines (49 loc) · 2.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package ru.javawebinar.topjava.web.user;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import ru.javawebinar.topjava.model.User;
import java.net.URI;
import java.util.List;
/**
* GKislin
* 06.03.2015.
*/
@RestController
@RequestMapping(AdminRestController.REST_URL)
public class AdminRestController extends AbstractUserController {
static final String REST_URL = "/rest/admin/users";
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<User> getAll() {
return super.getAll();
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public User get(@PathVariable("id") int id) {
return super.get(id);
}
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> createWithLocation(@RequestBody User user) {
User created = super.create(user);
URI uriOfNewResource = ServletUriComponentsBuilder.fromCurrentContextPath()
.path(REST_URL + "/{id}")
.buildAndExpand(created.getId()).toUri();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setLocation(uriOfNewResource);
return new ResponseEntity<>(created, httpHeaders, HttpStatus.CREATED);
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public void delete(@PathVariable("id") int id) {
super.delete(id);
}
@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public void update(@RequestBody User user, @PathVariable("id") int id) {
super.update(user, id);
}
@RequestMapping(value = "/by", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public User getByMail(@RequestParam("email") String email) {
return super.getByMail(email);
}
}