Skip to content

Commit

Permalink
Validation example
Browse files Browse the repository at this point in the history
  • Loading branch information
jwierzbo committed Feb 8, 2018
1 parent cf57393 commit 1ef4645
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 8 deletions.
14 changes: 8 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>

<groupId>net.jwierzbo.rest</groupId>
<artifactId>REST-movie-service</artifactId>
<version>0.1</version>
Expand All @@ -19,12 +13,20 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<swagger.lib.version>2.8.0</swagger.lib.version>
<spring.boot.version>1.5.10.RELEASE</spring.boot.version>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@

import java.util.List;

/* This is example Controller class with extends description for SwaggerApi
/*
* This is example Controller class with extended description for SwaggerApi
* These @Api*** annotations are not necessary if we use @ResponseStatus
* and return Model objects instead of generic response
* and @ResponseBody objects instead of generic response
*/

@Api(value="MovieController", description="List of favourite Movies")
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/net/jwierzbo/rest/api/MovieV3ValidController.java
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);
}
}
7 changes: 7 additions & 0 deletions src/main/java/net/jwierzbo/rest/model/Movie.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import net.jwierzbo.rest.validation.Name;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;


@ApiModel(value = "Movie")
public class Movie {

@ApiModelProperty(notes = "The database generated product ID")
private Long id;

// example of default Bean validation
@NotNull(message = "title can not be null")
private String title;

// example of custom Bean validation
@Name(message = "Invalid director name - it has to start with uppercase!")
private String director;

@JsonFormat(pattern = "yyyy-MM-dd")
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/net/jwierzbo/rest/validation/Name.java
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 src/main/java/net/jwierzbo/rest/validation/NameValidator.java
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));
}
}

0 comments on commit 1ef4645

Please sign in to comment.