简体   繁体   English

springboot 中 REST api 的多个端点

[英]Multiple endpoints for REST api in springboot

I have two unique keys in a table id , userId .我在表id 中有两个唯一键userId I have to create a REST api in spring to get user details if any of the two keys are given as path variables.如果两个键中的任何一个作为路径变量给出,我必须在 spring 中创建一个 REST api 以获取用户详细信息。

The challenge here is we have to create two different endpoints for getting user through id and getting user through userId but use same method for both.这里的挑战是我们必须创建两个不同的端点,用于通过id获取用户和通过userId获取用户,但对两者使用相同的方法。 Also datatype of id is long and datatype of userId is String in table id 的数据类型也很长userId 的数据类型是表中的字符串

So I am trying to do following所以我正在尝试做以下

the endpoint "/user/{id}" is for userId端点“/user/{id}”用于 userId

@RequestMapping(value = {"/{id}","/user/{id}"}, method=RequestMethod.GET)
public response getUser(@PathVariable("id") String id){

}

But I am unable to figure out how to check whether I got id or userId inside the method.但是我无法弄清楚如何检查我是否在方法中获得了iduserId Also is this the right way to do it?这也是正确的方法吗?

You can do this with single method like this:你可以用这样的单一方法来做到这一点:

@RequestMapping(value = {"/{id}", "/user/{userId}"}, method = RequestMethod.GET)
public void getUser(@PathVariable(value = "id", required = false) String id,
  @PathVariable(value = "userId", required = false) Long userId) {
    if (id != null) {
    //TODO stuff for id
   }
   if (userId != null) {
    //TODO stuff for userId
   }

} }

I would use the @RequestMapping Multiple paths mapped to the same controller method possibility in such a manner.我会以这种方式使用映射到相同控制器方法可能性的@RequestMapping Multiple 路径。

I suspect that even if you refactor your code to call one single method, you still have to implement some logic to differentiate between the two parameters inside the controller method.我怀疑即使你重构你的代码来调用一个单一的方法,你仍然需要实现一些逻辑来区分控制器方法中的两个参数。

Besides, getUserById() signature is ambiguous.此外, getUserById()签名不明确。 What the parameter id means the id or userId参数id表示iduserId

Having two separate method for what you want to acheive would be more efficient two handle each case properly.为您想要实现的目标使用两种单独的方法会更有效,两种方法正确处理每种情况。 Inside each controller method you can use a common logic for the two if you want.如果需要,您可以在每个控制器方法中为两者使用通用逻辑。

@RequestMapping(value = "/user/{userId}", method=RequestMethod.GET)
public String getUserById(@PathVariable("userId") String userId){
   // Common login
}

@RequestMapping(value = "/id", method=RequestMethod.GET)
public String getUserByUserId(@PathVariable("userId") String userId){
   // Common login
}

You can even implement for each endpoint validators to check either you @PathVariable is valid or not in case of Long or String您甚至可以为每个端点验证器实现以检查 @PathVariable 在LongString情况下是否有效

Here are some references ref1 , ref2这里有一些参考文献ref1 , ref2

Aren't you able to refactor the database to have only one id?你不能将数据库重构为只有一个 id 吗? That makes things clear and keeps the code cleaner.这使事情变得清晰并保持代码更清晰。

If that is not possible you can create 2 methods with meaningful names.如果这是不可能的,您可以创建 2 个具有有意义名称的方法。

// handles: /user/{entityId}
@RequestMapping(value = "/user/{entityId}", method=RequestMethod.GET)
public UserDto getUserByEntityId(@PathVariable("entityId") long entityId){
   // call service 
}

// handles: /user?userId={userId}
@RequestMapping(value = "/user", method=RequestMethod.GET)
public UserDto getUserByUserId(@RequestParam("userId", required=true) String userId){
   // call service 
}

You can discuss about about the correct names/signatures of the methods.您可以讨论方法的正确名称/签名。

Another advantages of this approach is that you are able to add Swagger doc annotations to each of them.这种方法的另一个优点是您可以为它们中的每一个添加 Swagger 文档注释。

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

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