当前位置: 首页 > 知识库问答 >
问题:

Spring获得相同的URL,但参数类型不同

陈茂
2023-03-14

因此,我正在创建一个用户API,并尝试在登录前调用getUserByEmail()。我的问题是,我得到了一个不明确的处理程序方法映射为HTTP路径错误。

@RequestMapping(value = "/user/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> getUser(@PathVariable("id") int id) {
    System.out.println("Fetching User with id " + id);
    User user = userService.findById(id);
    if (user == null) {
        System.out.println("User with id " + id + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<User>(user, HttpStatus.OK);
}

@RequestMapping(value = "/user/{email}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> getUserByEmail(@PathVariable("email") String email) {
    System.out.println("Fetching User with email " + email);
    User user = userService.findByEmail(email);
    if (user == null) {
        System.out.println("User with email " + email + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<User>(user, HttpStatus.OK);
}

我知道我的问题与我有一个相同但参数类型不同的GET有关。任何帮助都将不胜感激!

共有1个答案

孟成化
2023-03-14

您可以使用带有两个不同URL的RequestParams,而不是PathVariables。

@RequestMapping(value = "/user", params="userID", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> getUser(@RequestParam("userID") int id) {
    System.out.println("Fetching User with id " + id);
    User user = userService.findById(id);
    if (user == null) {
        System.out.println("User with id " + id + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<User>(user, HttpStatus.OK);
}

@RequestMapping(value = "/user", params="emailID, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE")
public ResponseEntity<User> getUserByEmail(@RequestParam("emailID") String email) {
    System.out.println("Fetching User with email " + email);
    User user = userService.findByEmail(email);
    if (user == null) {
        System.out.println("User with email " + email + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<User>(user, HttpStatus.OK);
}

当您调用/user?userid=123时,它将调用第一个,而当您调用/user?emailid=something@gamil.com时,它将调用第二个。这由在@requestmapping中传递的params属性负责。

 类似资料:
  • 我有个奇怪的问题。我有三节课。书(摘要)和两个子类(小说、非小说类)。我已经创建了一系列参考书。该数组可以容纳两个子类的对象。我有以下小说课的代码。 } 非小说类的“相同”代码 } 这是主程序。 公共课主课{ 公共静态void main(字符串[]args){ } 出于任何原因,我在这一行得到ArrayStore异常... 但同样的代码也适用于小说类。我查看了java文档,它说当我试图在数组中存储

  • 我在Java中有两个几乎相同的方法。唯一的区别是它们有不同的参数类型。它们使用泛型并返回输入参数的类型T。我怎样才能摆脱重复的代码?下面是我的两个方法。最后,它们都使用不同的类型调用Spring。否则,方法是相同的。

  • 问题内容: 我在这里已经读到,在Java中,具有相同名称但不同类型的两个变量可以在同一范围内共存。我的意思是这个 但是所有的Java IDE都不允许这样的代码。我想知道这样的代码在语法上是否正确,或者只是IDE不允许这样的代码防止歧义。 无论如何,这是网站的摘录 “如果幸运的话,您也许能够重新编译Jad的输出。 但是,Java VM对于变量命名的规则比Java语言本身更为宽松。例如,一个有效的类文

  • 但所有java IDE都不允许这样的代码。我想知道这样的代码在语法上是否真的正确,或者只是IDE不允许这样的代码来防止歧义。 总之,这里是从网站上摘录的 “如果你幸运的话,你也许可以重新编译JAD的输出。然而,Java VM对变量命名的规则比Java语言本身更宽松。例如,一个有效的类文件可以有几个名为'a'的变量,只要它们有不同的类型。如果你反编译这样的类,你得到的源代码将是无效的。 JAD通常会

  • 问题内容: 假设我有一个域类: 其中Animal是具有不同实现(Cat,Dog)的接口。假设我希望能够保存Zoo对象: 我想发送一个json-类似: 我如何告诉Spring MVC在type ==’Cat’时将动物映射到Cat类型,在type ==’Dog’时将其映射到Dog类? 问题答案: 您应该使用Jackson注释并实现多态json。注释在基类上。

  • 假设我有一个域类: 其中动物是具有不同实现的接口(猫、狗)。假设我希望能够保存一个动物园对象: