Skip to content

Instantly share code, notes, and snippets.

@matsev
Created June 17, 2012 10:16
Show Gist options
  • Select an option

  • Save matsev/2944122 to your computer and use it in GitHub Desktop.

Select an option

Save matsev/2944122 to your computer and use it in GitHub Desktop.

Revisions

  1. matsev revised this gist Jun 17, 2012. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions CreateUser.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    @RequestMapping(method = RequestMethod.POST,
    consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    @ResponseStatus(HttpStatus.CREATED)
    void create(@RequestBody @Valid User user, HttpServletRequest request, HttpServletResponse response) {
    long userId = userService.create(user);
    String location = ServletUriComponentsBuilder.fromRequestUri(request).path("/{userid}")
    .build()
    .expand(userId).toUriString();
    response.addHeader("Location", location);
    }
  2. matsev revised this gist Jun 17, 2012. 1 changed file with 34 additions and 0 deletions.
    34 changes: 34 additions & 0 deletions User.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    @XmlRootElement
    public class User {

    @NotBlank
    @Length(min = 3, max = 30)
    private String name;

    @Email
    private String email;

    public User() {
    }

    public User(String name, String email) {
    this.name = name;
    this.email = email;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getEmail() {
    return email;
    }

    public void setEmail(String email) {
    this.email = email;
    }
    }
  3. matsev revised this gist Jun 17, 2012. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions ReadUser.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    @RequestMapping(value = "/{userId}",
    method = RequestMethod.GET,
    produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    @ResponseBody
    User read(@PathVariable("userId") long userId) {
    return userService.read(userId);
    }
  4. matsev revised this gist Jun 17, 2012. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions UpdateUser.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    @RequestMapping(value = "/{userId}",
    method = RequestMethod.PUT,
    consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    @ResponseStatus(HttpStatus.NO_CONTENT)
    void update(@PathVariable("userId") long userId, @RequestBody @Valid User user) {
    userService.update(userId, user);
    }
  5. matsev revised this gist Jun 17, 2012. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion DeleteUser.java
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    @RequestMapping(value = "/{userId}",
    method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    void delete(@PathVariable("userId") int userId) {
    void delete(@PathVariable("userId") long userId) {
    userService.delete(userId);
    }
    2 changes: 1 addition & 1 deletion UserController.java
    Original file line number Diff line number Diff line change
    @@ -9,5 +9,5 @@ class UserController {
    this.userService = userService;
    }

    // Add CRUD methods
    // TODO implement CRUD methods
    }
  6. matsev revised this gist Jun 17, 2012. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions DeleteUser.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    @RequestMapping(value = "/{userId}",
    method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    void delete(@PathVariable("userId") int userId) {
    userService.delete(userId);
    }
  7. matsev revised this gist Jun 17, 2012. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions UserController.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    @Controller
    @RequestMapping("/user")
    class UserController {

    private final UserService userService;

    @Autowired
    UserController(UserService userService) {
    this.userService = userService;
    }

    // Add CRUD methods
    }
  8. matsev created this gist Jun 17, 2012.
    10 changes: 10 additions & 0 deletions UserService.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    public interface UserService {

    long create(User user);

    User read(long userId) throws UnknownUserException;

    void update(long userId, User user) throws UnknownUserException;

    void delete(long userId) throws UnknownUserException;
    }