-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jwierzbo
committed
Feb 8, 2018
1 parent
cf57393
commit 1ef4645
Showing
6 changed files
with
111 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/net/jwierzbo/rest/api/MovieV3ValidController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package net.jwierzbo.rest.api; | ||
|
||
import net.jwierzbo.rest.dao.MovieDAO; | ||
import net.jwierzbo.rest.model.Movie; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.validation.Valid; | ||
import java.util.List; | ||
|
||
/* | ||
* This is example of Validation flow in RestController | ||
*/ | ||
|
||
@RestController | ||
@RequestMapping("/v3") | ||
public class MovieV3ValidController { | ||
|
||
@Autowired | ||
private MovieDAO movieDAO; | ||
|
||
@GetMapping("/movies") | ||
public List<Movie> getMovies() { | ||
return movieDAO.list(); | ||
} | ||
|
||
@GetMapping("/movies/{id}") | ||
public Movie getMovie(@PathVariable("id") Long id) { | ||
return movieDAO.get(id).get(); | ||
} | ||
|
||
@PostMapping(value = "/movies") | ||
@ResponseStatus(value = HttpStatus.CREATED) | ||
public Movie createMovie(@Valid @RequestBody Movie movie) { // Validate input | ||
return movieDAO.create(movie); | ||
} | ||
|
||
@DeleteMapping("/movies/{id}") | ||
@ResponseStatus(value = HttpStatus.NO_CONTENT) | ||
public void deleteMovie(@Valid @PathVariable Long id) { // Validate input | ||
movieDAO.delete(id); | ||
} | ||
|
||
@PutMapping("/movies/{id}") | ||
public Movie updateMovie(@PathVariable Long id, @RequestBody Movie movie) { | ||
return movieDAO.update(id, movie); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package net.jwierzbo.rest.validation; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Documented | ||
@Constraint(validatedBy = NameValidator.class) | ||
@Target( { ElementType.METHOD, ElementType.FIELD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Name { | ||
|
||
String message() default "{Name}"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/net/jwierzbo/rest/validation/NameValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package net.jwierzbo.rest.validation; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
public class NameValidator implements ConstraintValidator<Name, String> { | ||
public void initialize(Name constraintAnnotation) { | ||
} | ||
|
||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
return !value.isEmpty() && Character.isUpperCase(value.charAt(0)); | ||
} | ||
} | ||
|