实践Spring之Validation

关键字

ValidationException

ConstraintViolationException

org.springframework.validation.annotation.Validated

javax.validation.Valid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.concurrent.TimeUnit;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import wang.ray.dubbo.sample.BookRequest;
import wang.ray.dubbo.sample.BookResponse;
import wang.ray.dubbo.spring.boot.starter.sample.provider.service.SampleService;

@Service
@Validated
public class SampleServiceImpl implements SampleService {

    Logger logger = LoggerFactory.getLogger(SampleServiceImpl.class);

    @Override
    public BookResponse getBook(@Valid BookRequest bookRequest) {
        logger.info("BookRequest: {}", bookRequest);
        BookResponse bookResponse = new BookResponse();
        bookResponse.setId(bookRequest.getId());
        bookResponse.setName(bookRequest.getName());
        bookResponse.setResult("answer");
        bookResponse.setPageList(bookRequest.getPageList());
        return bookResponse;
    }

}
1
2
3
4
5
6
7
8
9
10
11
try {
// 需要参数校验的方法
} catch (ConstraintViolationException cve) {
	String message = "";
	Set<ConstraintViolation<?>> constraintViolations = cve.getConstraintViolations();
	for (ConstraintViolation<?> constraint : constraintViolations) {
		message = constraint.getMessage();
		break;
    }
	logger.error(message);
}