Last active
April 30, 2019 12:56
-
-
Save hejcz/de60637e529c382816fa40471d65e52c to your computer and use it in GitHub Desktop.
validation-jackson
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
package io.github.hejcz; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import org.hibernate.validator.constraints.ISBN; | |
import javax.validation.Valid; | |
import javax.validation.Validation; | |
import javax.validation.Validator; | |
import javax.validation.constraints.NotEmpty; | |
import javax.validation.constraints.NotNull; | |
import javax.validation.constraints.Pattern; | |
import javax.validation.constraints.Size; | |
import java.io.IOException; | |
import java.util.List; | |
class AuthorizationRequest { | |
@NotEmpty | |
@Size(max = 4) | |
public List<Privilege> privilegeList; | |
@NotNull(message = "responseType must not be null") | |
@Pattern(regexp = "code", message = "responseType must be 'code'") | |
public String responseType; | |
@Valid | |
@JsonProperty("hello") | |
public Hello hello; | |
@NotNull | |
@ISBN | |
public String number; | |
} | |
class Privilege { | |
} | |
class Hello { | |
@NotNull | |
public String responseType; | |
} | |
public class App { | |
public static void main(String[] args) throws IOException { | |
ObjectMapper om = new ObjectMapper(); | |
AuthorizationRequest req = om.readValue( | |
"{\n" + | |
" \"privilegeList\": [],\n" + | |
" \"responseType\": \"code\",\n" + | |
" \"number\": \"978-1-56619-909-4\"\n" + | |
"}", | |
AuthorizationRequest.class); | |
System.out.println(req); | |
Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); | |
System.out.println(validator.validate(req)); | |
} | |
} |
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
plugins { | |
id 'java' | |
} | |
group 'io.github.hejcz' | |
version '1.0-SNAPSHOT' | |
sourceCompatibility = 1.8 | |
repositories { | |
maven { | |
url = "url-to-artifactory" | |
} | |
} | |
dependencies { | |
// util | |
compileOnly 'org.projectlombok:lombok:1.18.6' | |
annotationProcessor 'org.projectlombok:lombok:1.18.6' | |
// json | |
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.8' | |
// validation | |
compile 'org.hibernate.validator:hibernate-validator:6.0.16.Final' | |
compile 'org.glassfish:javax.el:3.0.1-b09' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment