Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Validate the parameters of a service method void transfer(@NotNull String from, @Positive BigDecimal amount). What do you need?

We need a Bean Validation provider on the classpath (spring-boot-starter-validation) and @Validated on the service class. Spring Boot then auto-configures a MethodValidationPostProcessor, which wraps the bean in a proxy that validates the parameters before transfer runs and throws ConstraintViolationException when a rule fails. The check only happens for calls that go through the proxy, so a call from another method of the same class is not validated.

Why this question is asked

Validation is usually taught on controllers, with @Valid on a request body. But many calls never pass through a controller: a scheduled job, a message listener or another service may call transfer directly. Putting the rules on the service method protects every caller at once. Interviewers ask this to check that we know what turns those annotations on, because annotations alone do nothing, and whether we know the proxy trap that comes with it. In this answer, we will recap what method validation is, set it up step by step, see how the proxy runs the check, look at the exception we get, and then reproduce the self-invocation problem.

1A Quick Recap of Method Validation

Bean Validation, the Jakarta standard that Hibernate Validator implements, can validate more than object fields. Since version 1.1 it can also check the parameters and the return value of a method. The standard calls this executable validation, and it offers an ExecutableValidator that, given a method and its arguments, returns the list of violations.

Something has to call that validator before our method runs. In Spring, that something is a proxy: a wrapper object that Spring generates at runtime around our bean. Other beans receive the proxy instead of the real object, so the proxy sees every call and can run the check first. This is the same mechanism that makes @Transactional work.

2What We Need, Step by Step

First, a validation provider must be on the classpath. In Spring Boot that means the validation starter, which brings Hibernate Validator. Without a provider there is nothing to run the rules, and Boot does not create the post-processor at all.

Second, the class gets @Validated, Spring's own annotation. It is the marker that tells Spring this bean needs a validating proxy. The constraint annotations go on the parameters as written in the question.

Java
// build.gradle: implementation "org.springframework.boot:spring-boot-starter-validation"
@Service
@Validated // marks the bean for method validation
public class TransferService {
public void transfer(@NotNull String from, @Positive BigDecimal amount) {
// runs only after both parameters passed validation
}
}
// In a caller that received the bean by injection:
transferService.transfer("ACC-1", new BigDecimal("25.00")); // runs
transferService.transfer(null, new BigDecimal("25.00")); // ConstraintViolationException
transferService.transfer("ACC-1", BigDecimal.ZERO); // ConstraintViolationException: must be greater than 0

Third, something must create the proxy. Spring Boot's validation auto-configuration registers a MethodValidationPostProcessor bean as soon as a provider is present. A bean post-processor is a hook that Spring calls for every bean it creates; this one looks for @Validated and wraps those beans. Outside Spring Boot we would declare the MethodValidationPostProcessor bean ourselves.

3How the Proxy Runs the Check

When a caller invokes transfer, the call first reaches the proxy. The proxy hands it to a MethodValidationInterceptor, which asks the validator to check the arguments against the annotations on the method. If there are no violations, the interceptor lets the call continue to our real method. If there are, it throws before a single line of our code runs.

Step throughA validated call to transfer
CallerProxyValidatorTransferService
transfer(null, 25.00)
validate parameters
1 violation: from must not be null
throw ConstraintViolationException
transfer("ACC-1", 25.00)
validate parameters
no violations
transfer("ACC-1", 25.00)

8 messages in order. Play them, or step through them one at a time.

The real method is only reached when the arguments pass.

Notice the order. Validation happens before the method body, so the method can rely on from being non-null and amount being positive. If a method also has constraints on its return value, the interceptor checks those after the method returns.

4What a Violation Looks Like

The exception is jakarta.validation.ConstraintViolationException. It carries a set of ConstraintViolation objects, each with a property path, the invalid value and a message. For method parameters, the path starts with the method name followed by the parameter name.

Java
try {
transferService.transfer("ACC-1", new BigDecimal("-5"));
} catch (ConstraintViolationException ex) {
for (ConstraintViolation<?> v : ex.getConstraintViolations()) {
System.out.println(v.getPropertyPath() + ": " + v.getMessage());
// transfer.amount: must be greater than 0
}
}

The parameter name amount only appears if the code was compiled with the -parameters flag, which the Spring Boot Maven and Gradle setups enable by default. Without it, the path shows a generated name such as arg1. Also note that ConstraintViolationException is not mapped to an HTTP status by Spring MVC, so if such a call starts from a controller, we need an exception handler that turns it into a 400.

5The Trap: Calls From Inside the Same Class

Because the check lives in the proxy, a call that does not go through the proxy is not checked. The common case is one method calling another on this.

Java
@Service
@Validated
public class TransferService {
public void transferAll(List<Instruction> list) {
for (Instruction i : list) {
transfer(i.from(), i.amount()); // this.transfer(...): the proxy is skipped
}
}
public void transfer(@NotNull String from, @Positive BigDecimal amount) { ... }
}
transferService.transferAll(List.of(new Instruction(null, BigDecimal.ONE)));
// no exception: transfer ran with from == null

The reason is that transferAll runs inside the real object, so this refers to the real object and not to the proxy. The fix is the same as for @Transactional: put the validated method on a separate bean and inject it, or validate explicitly with an injected jakarta.validation.Validator when a self-call is unavoidable.

6Follow-up questions

How do we validate an object parameter such as a TransferRequest record?

We mark the parameter with @Valid, for example transfer(@Valid TransferRequest request). @Valid tells the validator to cascade into the object and check the constraints declared on its fields or record components. Without @Valid, only constraints placed directly on the parameter are checked.

Where should the annotations go if the service implements an interface?

On the interface method, or identically on both. Bean Validation forbids an overriding method from adding parameter constraints that the interface method does not have, because a caller using the interface could not know about them, and the provider reports a ConstraintDeclarationException. Return value constraints may be added in the implementation.

Can we validate only some rules for some callers?

Yes, with validation groups. @Validated accepts group classes, for example @Validated(OnCreate.class) on a method, and only constraints in those groups are checked for that method. This is useful but adds complexity, so we use it only when the same object really has different rules in different flows.

7Conclusion

In this answer, we saw what makes the annotations on transfer actually run. We need a validation provider, @Validated on the service class, and the MethodValidationPostProcessor that Spring Boot registers for us. The post-processor wraps the bean in a proxy that validates arguments before the method body and throws ConstraintViolationException with paths like transfer.amount. Because the check lives in the proxy, a call from another method of the same class skips it.

What to say in the interviewAbout 80 seconds aloud

Open with

Constraint annotations on a method do nothing by themselves; something has to run the validator. In Spring Boot I add the validation starter, which brings Hibernate Validator, and then I put @Validated on the service class.

Then, if they want more

Because a provider is present, Boot auto-configures a MethodValidationPostProcessor. It wraps every @Validated bean in a proxy with a MethodValidationInterceptor, so when someone calls transfer through the injected bean, the interceptor validates the parameters first: from must not be null and amount must be positive.

If a rule fails, it throws ConstraintViolationException, which holds each violation with a path like transfer.amount and the message, and the method body never runs. Two things to keep in mind. First, that exception is not turned into a 400 automatically, so if the call starts in a controller I map it in an exception handler.

Second, the check lives in the proxy, so if another method in the same class calls transfer on this, validation is skipped. If I need that, I move the method to another bean or call an injected Validator explicitly. For object parameters I add @Valid so the validator cascades into the object.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Validate the parameters of a service method void transfer(@NotNull String from...": https://scrivio-interview-library.netlify.app/spring-boot/request-validation/a59cf5894a/