Tôi nhận được ngoại lệ sau đây nếu tôi sử dụng url đa cấp trong lớp như @RequestMapping("/api/v0.1")
:lập bản đồ mơ hồ nhận thấy khi sử dụng url @RequestMapping lớp đa mức
java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'userController'
bean method getUsers()|to {[/api/v0.1]}: There is already 'userController' bean
method getUser(java.lang.String) mapped.
Nó giống như ánh xạ mức độ phương pháp không được vào xem xét ở tất cả.
Nhưng không sao nếu tôi đặt @RequestMapping("/api")
tức là xóa phần /v0.1
.
Dưới đây là cấu hình tước đến trường hợp tối thiểu:
@Controller
@RequestMapping("/api/v0.1")
public class UserController {
@RequestMapping(value = "/users")
@ResponseBody
public List<User> getUsers() {
return null;
}
@RequestMapping(value = "https://stackoverflow.com/users/{username}")
@ResponseBody
public User getUser(@PathVariable String username) {
return null;
}
}
web.xml
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
servlet-context.xml:
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- Forwards requests to the "/" resource to the "welcome" view -->
<mvc:view-controller path="/" view-name="home"/>
<!-- Handles HTTP GET requests for /assets/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
<mvc:resources mapping="/assets/**" location="/assets/" />
Tôi đang sử dụng lò xo 3.1. Tôi cũng đã cố gắng đặt thuộc tính alwaysUseFullPath
thành true cho số hạt RequestMappingHandlerMapping
nhưng không thay đổi tình hình.
Tôi nhớ đã cố gắng '@RequestMapping ("/api/v01 ")' trên cấp lớp (không có dấu chấm) và không nhận được kết quả nhưng tôi sẽ thử lại và cho bạn biết. cảm ơn. – SelimOber
Ok, tôi đã thử v0_1 và nó hoạt động. Vì vậy, tôi sẽ đi với cái này. – SelimOber
Vì AntPathMatcher cho phép biểu thức chính quy, tôi nghĩ nếu bạn thoát khỏi dấu chấm thì nó sẽ hoạt động. ví dụ. 'v0 \ .1' – jasop