Is it possible to verify empty input from the user in spring boot?

shay n :

I'm writing a spring boot application and I'm having some troubles in verifying empty input from the user. Is there a way to validate an empty input from the user? For example:

@PostMapping("/new_post/{id}")
public int addNewPost(@PathVariable("id") Integer id, @RequestBody Post post) {
    return postService.addNewPost(id, post);
}`

Here I want to add a new post only if the user exists in the database but when I send this post request I am getting the regular 404 error message and I am not able to provide my own exception although in my code I validate if the id equals to null.

http://localhost:8080/new_post/

Any idea what can I do?

Thanks

pvpkiran :

You can do something like this

@PostMapping(value = {"/new_post/{id}", "/new_post"})
public int addNewPost(@PathVariable(required = false, name="id") Integer id, @RequestBody Post post) {
    return postService.addNewPost(id, post);
}

But the ideal way to handle this is using @RequestParam. @RequestParam is meant exactly for this purpose.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=409537&siteId=1