1

I have a spring-boot:1.2.3 application and I'm having 2 validation issues:

  • it works in @Path endpoints but not @Service or @Component classes
  • it doesn't throw or log anything if the validation fails, it just returns a 400 without any additional information

These are the validation libraries that already come with spring-boot:1.2.3:

[INFO] |  +- org.hibernate:hibernate-validator:jar:5.1.3.Final:compile
[INFO] |  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  +- org.glassfish.jersey.ext:jersey-bean-validation:jar:2.14:compile

These are the logging libraries that come with spring-boot-starter-log4j:

[INFO] +- org.springframework.boot:spring-boot-starter-log4j:jar:1.2.2.RELEASE:compile
[INFO] |  +- org.slf4j:slf4j-log4j12:jar:1.7.10:compile
[INFO] |  \- log4j:log4j:jar:1.2.17:compile

This validation works but doesn't log or show any stacktrace:

@Component
@Path("/validation-example")
public class ValidationExampleResource {

    @GET
    @Path("/{positiveNumber}")
    public int validationExample(@Min(0) @PathParam("positiveNumber") int positiveNumber) {
        ...
    }
}

But the validation doesn't work here at all:

@Service
public class ValidationExampleServiceImpl implements ValidationExampleService {

    @Override
    public int validationExample(@Min(0) int positiveNumber) {
        ...
    }
}
5
  • unfortunately no. If you figure it out, please let us know. I'm guessing this would work if I used Spring MVC controllers instead of JAX-RS Resource classes, but I'd like this to work with both, of course. Commented Dec 15, 2015 at 9:40
  • Which is weird is that I can't get it to work with Path endpoints Commented Dec 15, 2015 at 10:15
  • What jersey version are you using? Commented Dec 15, 2015 at 14:01
  • 2.14 as set by spring-boot-dependencies:1.2.3.RELEASE Commented Dec 15, 2015 at 14:08
  • 1
    you have missed the @ Valid.Please try with @ valid before min. Commented Dec 11, 2017 at 6:58

2 Answers 2

1

Try this config. In my project it worked! Spring boot: 1.5.9.RELEASE

@Slf4j
@Service
@Validated
public class ValidationService {

    @Validated(CreateGroup.class)
    public void create(@Valid User user) {
        log.info("验证服务业务的作用");
    }

    public void basic(@NotNull(message = "名称不能为空") String name) {

    }

}

Details

Sign up to request clarification or add additional context in comments.

Comments

0

In order for the validation to work, you need to add @Valid

@Component
@Path("/validation-example")
public class ValidationExampleResource {

    @GET
    @Path("/{positiveNumber}")
    public int validationExample(@Valid @Min(0) @PathParam("positiveNumber") int positiveNumber) {
        ...
    }
}

1 Comment

@cahen here is an example fbflex.wordpress.com/2014/06/02/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.