简体   繁体   English

Spring:使用BadRequest处理模棱两可的映射

[英]Spring: handle ambiguous mapping with BadRequest

I'm developing a Spring application, and at the moment I'm implementing a search on our customers registry. 我正在开发Spring应用程序,此刻正在对客户注册表进行搜索。

A key-pointy of our users request is that the search can be done for at most ONE of the following parameters : name OR id (can be partial) OR shop id (the shop where the customer is registered) Searches with more parameters ("more filters" if you prefer) in combination, are not allowed. 我们的用户请求的关键尖尖的是,搜索可以为下列参数最多之一来进行:名称 ID(可以是部分的) 商店ID(其中客户登记店铺) 与多个参数搜索(”如果您愿意,可以组合使用更多过滤器”。

Here i ran into the problem: I developed a RestController with 1 endpoint handled by 3 different functions like this: 在这里,我遇到了一个问题:我开发了一个带有1个端点的RestController,该端点由3种不同的函数处理,如下所示:

@RestController()
    @RequestMapping("v1/customers")
    public class ExampleController {

        /**
        * Search for customer's name (can be partial)
        * @param name
        * @return
        */
        @GetMapping(value="search", params="name")
        public String searchByName(String name) {
            return "search for name";
        }


        /**
        * Search for the customer by id (can be partial)
        * @param name
        * @return
        */
        @GetMapping(value="search", params="id")
        public String searchById(String id) {
            return "search for id";
        }

        /**
        * search for the customer from the shop id where he is registered
        * @param name
        * @return
        */
        @GetMapping(value="search", params="shopId")
        public String searchByShopID(String shopId) {
            return "search for shop";
        }

    }

All works fine except for a problem: 一切正常,除了问题:

The problem is that if the application recive a request with multiple query params like this: 问题是,如果应用程序接收具有多个查询参数的请求,如下所示:

http://localhost:8080/v1/customers/search?name=bob&shopId=897a

Spring throw an exception of "Ambiguous Mapping" and return an HTTP 500 status code Spring引发“模棱两可的映射”异常,并返回HTTP 500状态代码

How can i handle it? 我该如何处理? It's possible to handle the case returning an HTTP 400 (and a custom message)? 可以处理返回HTTP 400(和自定义消息)的情况? Maybe with something like a "Default mapping". 可能带有“默认映射”之类的内容。

You can use https://docs.spring.io/spring/docs/5.0.8.RELEASE/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#params-- parameter to filter by HTTP parameters. 您可以使用https://docs.spring.io/spring/docs/5.0.8.RELEASE/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#params--参数按HTTP参数进行过滤。 In your case it would be something like: 在您的情况下,它将类似于:

 @GetMapping(value="search",params = "name")
        public String searchByName(@RequestParam("name") String name) {
            return "search for name";
        }

 @GetMapping(value="search",params = "shopId")
    public String searchByShopID(@RequestParam("shopId") String shopId) {
        return "search for shop";
    }

Thank to @GauravRai1512 i read another time the Spring's documentation, and i noticed that with "params" i can filter for "absent" paramenters too! 感谢@ GauravRai1512,我又读了一次Spring的文档,我注意到有了“ params”,我也可以过滤掉“ params”参数! (i didn't notice at first time) this way params="!myParam" (我第一次没有注意到)以这种方式params="!myParam"

So i tried to do it this way an it works: 所以我尝试用这种方式来做到这一点:

(Let me know your opionion :) ) (让我知道您的选择:))

@RestController()
@RequestMapping("v1/customers")
public class ExampleController {

    /**
    * Search for customer's name (can be partial)
    * @param name
    * @return
    */
    @GetMapping(value="search", params={"name", "!id", "!shopId"})
    public String searchByName(String name) {
        return "search for name";
    }


    /**
    * Search for the customer by id (can be partial)
    * @param name
    * @return
    */
    @GetMapping(value="search",  params={"!name", "id", "!shopId"})
    public String searchById(String id) {
        return "search for id";
    }

    /**
    * search for the customer from the shop id where he is registered
    * @param name
    * @return
    */
    @GetMapping(value="search", params={"!name", "!id", "shopId"})
    public String searchByShopID(String shopId) {
        return "search for shop";
    }

    /**
    * Handle invalid reqest
    * @param name
    * @return
    */
    @GetMapping(value="search")
    public String searchByShopID() {
        return "Invalid!";
    }

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM