Spring boot 模糊处理程序 [英] Spring boot Ambiguous handler

查看:30
本文介绍了Spring boot 模糊处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Spring Boot.我创建了这两种方法:

I use Spring Boot. I created theses two method:

@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
public UserAppDto getNameByUserId(@PathVariable("userId") Long userId) {
    return userService.getByUserId(userId);
}

@RequestMapping(value = "/user/{username}", method = RequestMethod.GET)
public UserAppDto getNameByUsername(@PathVariable("username") String username) {
    return userService.getNameByUsername(username);
}

当我尝试登录 Web 应用程序时,我得到:

When i try to log in the web application, i get:

java.lang.IllegalStateException:映射的模糊处理程序方法HTTP 路径 'http://localhost:8080/rest/user/bsmith': {publiccom.zenar.dto.UserAppDtocom.zenar.controller.UserController.getNameByUsername(java.lang.String),公共 com.zenar.dto.UserAppDtocom.zenar.controller.UserController.getNameByUserId(java.lang.Long)}

java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path 'http://localhost:8080/rest/user/bsmith': {public com.zenar.dto.UserAppDto com.zenar.controller.UserController.getNameByUsername(java.lang.String), public com.zenar.dto.UserAppDto com.zenar.controller.UserController.getNameByUserId(java.lang.Long)}

好像不能在数据类型上做区别.

Seem like, it's not able to do difference on the data type.

所以需要修改网址?最新版本中是否有任何修复?

So need to modify URL? Any fix in the latest release?

推荐答案

根据 Spring MVC 文档,当一个 URL 匹配多个模式时,使用排序来查找最具体的匹配:

According to Spring MVC documentation, when a URL matches multiple patterns, a sort is used to find The Most Specific Match:

URI 变量和通配符数量较少的模式是考虑更具体.例如 /hotels/{hotel}/* 有 1 个 URI变量和 1 个通配符,被认为比/hotels/{hotel}/** 作为 1 个 URI 变量和 2 个通配符.

A pattern with a lower count of URI variables and wild cards is considered more specific. For example /hotels/{hotel}/* has 1 URI variable and 1 wild card and is considered more specific than /hotels/{hotel}/** which as 1 URI variable and 2 wild cards.

如果两个模式的计数相同,则较长的一个是考虑更具体.例如 /foo/bar* 更长并且被认为比 /foo/* 更具体.

If two patterns have the same count, the one that is longer is considered more specific. For example /foo/bar* is longer and considered more specific than /foo/*.

当两个图案的数量和长度相同时,更少的通配符被认为更具体.例如/hotels/{hotel}/hotels/* 更具体.

When two patterns have the same count and length, the pattern with fewer wild cards is considered more specific. For example /hotels/{hotel} is more specific than /hotels/*.

应用这些规则后,当 Spring MVC 无法决定哪一个更具体时,它会抛出该异常.解决此问题的一种方法是使其中一个更具体:

After applying these rules, when Spring MVC couldn't decide which one is more specific, it will throw that exception. One way for fixing this problem is to make one of them More specific:

@RequestMapping(value = "/user/{userId:\\d+}", method = RequestMethod.GET)
public UserAppDto getNameByUserId(@PathVariable("userId") Long userId) {
    return userService.getByUserId(userId);
}

这篇关于Spring boot 模糊处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆