3

how to validate a spring boot rest endpoint with a String parameter, here is the sample of my endpoint.

@ResponseBody
@RequestMapping(value = "/getProfile", method = RequestMethod.POST,  produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> getProfile(@RequestBody @Valid @NotEmpty String profileId){}

even thought i added @valid and @NonEmpty constraint its not getting validated.

but its working for a other classes. for eg,

@ResponseBody
    @RequestMapping(value = "/saveProfiles", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Object> saveProfiles(@RequestBody @Valid PersistProfilesRequest request){}

here all the fields in PersistProfilesRequest class which have a constrain applied is validating properly.

validation is not happening for method parameter with java class like String and Map.

i am using spring boot 1.3.0 with hibernate validator.

how can i add validation to a rest endpoint with a string parameter?

EDIT

i am using junit and mockmvc to test the endpoint, below is the test case

@Test
public void testGetProfile() throws Exception{

    String profileId = "   ";

    this.mockMvc.perform(post("/getProfile").content(profileId).accept(MediaType.APPLICATION_JSON).contentType("application/json"))
                .andDo(print())
                .andExpect(status().isInternalServerError());

}
10
  • is profileId coming as requestBody ? Commented Nov 7, 2018 at 16:07
  • yes profileId is coming as request body, its just string not a key value pair Commented Nov 7, 2018 at 16:12
  • Could you show requestBody call with javascript please? Commented Nov 7, 2018 at 16:23
  • Hello there. Could you please explain what you're expecting about @RequestBody @Valid String ? What is the validation ? I think you should use @RequestBody(required = true) instead. Commented Nov 7, 2018 at 16:27
  • Mickael, "public @interface RequestBody { boolean required() default true; }" Its by default true Commented Nov 7, 2018 at 16:28

1 Answer 1

1

You can try to annotate your controller class with @Validated. In my case it helped.

@Validated
@RestController
@RequestMapping(path = "/api/somethin")
public class LegacyRestController {
Sign up to request clarification or add additional context in comments.

5 Comments

is the java validation framework and the hibernate Validator work only for bean ?
No actually not. Validation annotations and their validators are used mostly for base types. @NotEmpty can be used for strings and collections. In your case there is something different cause to not validate those parameters. Tomorrow I can look my own project again if I did some extra configuration for that.
Oh I see the problem, try to change your method as GET (instead of Post) and change replace '@RequestBody' with '@RequestParam'.
here is the annotation in controller class, --- Validated RestController RequestMapping("/kvassist") ComponentScan(basePackages = { "com.app.profile" })
updated endpoint declaration to this -- ResponseBody RequestMapping(value = "/getProfile", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE, consumes=MediaType.TEXT_PLAIN_VALUE) public ResponseEntity<Object> getProfile(@RequestParam(name="p") Valid Email String profileId)

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.