me hungry!
  • migrate to herdin.github.io
  • DEV TOOLS
    • IDE
      • VSCode
        • spring-boot maven config
  • Spring
    • default
      • IoC
      • resource
      • validation
      • data bind
      • SpringEL
      • DispatcherServlet
    • debug
      • mariadb connection time out - connection pool
  • Database
    • MariaDB
      • command history
  • LINUX
    • CentOS
      • install java
      • install, setting maven
      • install, setting vault
      • upgrade docker to docker-ce
  • WINDOW
    • Road to window docker
  • Arduino
    • default
    • SENSORS
      • dust sensor
        • GP2Y1010AU0F
      • humidity&temperature
        • DHT11
      • lcd
        • SKU DFR0009 - shield
    • debug
Powered by GitBook
On this page

Was this helpful?

  1. Spring
  2. default

validation

부트를 안썻을때?

AppRunner.java
package herdin.boot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.Errors;

@Component
public class AppRunner implements ApplicationRunner {
	private Logger logger = LoggerFactory.getLogger(AppRunner.class);
	
	@Override
	public void run(ApplicationArguments args) throws Exception {
		Event event = new Event();
		event.setTitle("not empty fucker");
		EventValidator evnEventValidator = new EventValidator();
		Errors errors = new BeanPropertyBindingResult(event, "event");
		evnEventValidator.validate(event, errors);
		this.logger.debug("HAS ERROR? {}", errors.hasErrors());
		
		Thread.sleep(2000L);
		System.exit(0);
	}
}
package herdin.boot;

public class Event {
	Integer id;
	String title;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
}
package herdin.boot;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class EventValidator implements Validator {
	@Override
	public boolean supports(Class<?> clazz) {
		return Event.class.equals(clazz);
	}
	@Override
	public void validate(Object target, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "notempty", "empty title not allowed");
	}
}

부트를 썻을때?

AppRunner.java
package herdin.boot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

@Component
public class AppRunner implements ApplicationRunner {
	private Logger logger = LoggerFactory.getLogger(AppRunner.class);
	@Autowired
	Validator validator;
	
	@Override
	public void run(ApplicationArguments args) throws Exception {
		this.logger.debug("validator class is {}", this.validator.getClass());
		Event event = new Event();
		event.setSize(1);
		event.setTitle("not empty fucker");
		event.setEmail("noemailhaha@hasdf.com");
//		EventValidator evnEventValidator = new EventValidator();
		Errors errors = new BeanPropertyBindingResult(event, "event");
		validator.validate(event, errors);
		this.logger.debug("HAS ERROR? {}", errors.hasErrors());
		
		Thread.sleep(2000L);
		System.exit(0);
	}
}

부트를 사용하면 EventValidator 를 사용할 필요 없이 Validator 를 받아 사용하면 된다. 기본적으로 등록되는 Validator 는 org.springframework.validation.beanvalidation.LocalValidatorFactoryBean 이다.

Event.java
package herdin.boot;

import javax.validation.constraints.Email;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;

public class {
	Integer id;
	@NotEmpty
	String title;
	
	@Min(value=0)
	Integer size;
	
	@Email
	String email;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Integer getSize() {
		return size;
	}
	public void setSize(Integer size) {
		this.size = size;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
}
PreviousresourceNextdata bind

Last updated 5 years ago

Was this helpful?