0

please help me to solve the following issue: I have a class, where several fields are marked as @NotNull:

public class SearchCommentRequest {

@NotNull
private Date fromDate;

@NotNull
private Date toDate;

//...
}

Object if this class is passed to controller as @RequestBody annotated also with @Valid:

@PostMapping(value = "/comment/search", consumes="application/json", produces = "text/csv")
public ResponseEntity<byte[]> searchComments(@RequestBody @Valid SearchCommentRequest searchRequest) {
    List<SearchCommentResult> comments = commentService.searchComments(searchRequest);

So, I expect that if either fromDate or toDate is null - exception will be thrown.

Writing my integration tests, I decided to check this validation case as well:

@Test
public void searchCommentsValidateRequest() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SearchCommentRequest request = new SearchCommentRequest();
    // toDate = null; 
    request.setFromDate(new Date());
    String requestBody = mapper.writer().writeValueAsString(request);

    mockMvc.perform(post(COMMENT_SEARCH_ENDPOINT)
            .contentType("application/json")
            .content(requestBody))
            .andDo(MockMvcResultHandlers.print())
            .andExpect(status().is(400));
}

But it looks like mockMvc is ignoring validation. Searching for the same issues, I found several sources where solution was adding the following dependencies:

 <dependency>
    <groupId>javax.el</groupId>
    <artifactId>javax.el-api</artifactId>
    <version>2.2.4</version>
    <scope>test</scope>
 </dependency>

<dependency>
   <groupId>org.glassfish</groupId>
   <artifactId>javax.el</artifactId>
   <version>3.0.0</version>
</dependency>

But it didn't help. I'm using Spring 4.3.3.RELEASE and manually added to pom.xml the following dependency:

<dependency>
   <groupId>javax.validation</groupId>
   <artifactId>validation-api</artifactId>
   <version>2.0.1.Final</version>
</dependency>
Viacheslav
  • 143
  • 4
  • 19

1 Answers1

0

Actually, @Notnull is often use at entity level. It will validate and throw exception when you persist entity automatically.

If you want to validate at controller and use @Valid.

You should declare more about BindingResult result

and check errors

if(result.hasErrors()){
//do something or throw exceptions
}
Huy Nguyen
  • 1,931
  • 1
  • 11
  • 11
  • thanks, I've added @Valid to the property level to work in conjunction with NotNull, but the issue still persists, mockMvc is ignoring validation. BindingResult is the next step I think, it gives you a mechanism to decide what to do if validation error happened. As for now I don't need any custom logic, I just want the regular validation Exception to be thrown. – Viacheslav Aug 24 '18 at 08:37
  • Spring validator will validate your object and pass the result to bindingresult. https://spring.io/guides/gs/validating-form-input/ It don't throw exceptions here except you check and iterate error objects in binding result and throws exception. – Huy Nguyen Aug 25 '18 at 03:46
  • Not true, even without bindingresult org.springframework.web.bind.MethodArgumentNotValidException is thrown. Bidningresult is giving you a possibility to decide what to do with the errors (even ignore them), but it doesn't mean that without bindingresult no exception is thrown. – Viacheslav Aug 27 '18 at 08:25
  • I solved it by adding the following dependency: org.hibernate.validator / hibernate-validator – Benjamín Valero Dec 24 '22 at 14:13