在Spring Boot中,是否有可能验证用户的空输入?

huangapple go评论64阅读模式
英文:

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

问题

我正在编写一个Spring Boot应用程序,我在验证用户的空输入时遇到了一些问题。
有没有一种方法可以验证用户的空输入?
例如:

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

在这里,我只想在数据库中存在用户时才添加新的帖子,但是当我发送这个帖子请求时,我收到了常规的404错误消息,并且我无法提供自己的异常,尽管在我的代码中我验证了id是否为空。

http://localhost:8080/new_post/

有什么想法我该怎么办?

谢谢

英文:

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

答案1

得分: 1

我认为你需要这样做:

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

这样做可以处理 ID 为空时的 URL。

英文:

I think you need to do it like this:

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

This way you are also handling the URL when ID is null

答案2

得分: 1

你可以像这样做:

@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);
}

但处理这种情况的理想方式是使用@RequestParam@RequestParam 正好是为此目的而设计的。

英文:

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.

答案3

得分: 0

***我认为这是对另外两个更好的答案:***

    @PostMapping("/new_post/{id}")
    public int addNewPost(@PathVariable("id") Integer id, @RequestBody Post post)
    {
        if(!ObjectUtils.isEmpty(post))
        {
           return postService.addNewPost(id, post);
        }
        else 
         return null; // 您可以抛出异常或任何其他响应
    }

**id :** 不需要检查 'id',因为没有id,请求的方法不会被调用。
英文:

I Think This is a Better Answer for Two Others :

@PostMapping("/new_post/{id}")
public int addNewPost(@PathVariable("id") Integer id, @RequestBody Post post)
{
    if(!ObjectUtils.isEmpty(post))
    {
       return postService.addNewPost(id, post);
    }
    else 
     return null; // You can throws an Exception or any others response
}

id : Not required to check 'id', because with out id the requested method is not call.

huangapple
  • 本文由 发表于 2020年4月5日 20:07:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61042316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定