问题描述
我的DispatcherServlet无法正常工作.
My DispatcherServlet is not working correctly.
<servlet>
<servlet-name>userService</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/servlet/userService-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>userService</servlet-name>
<url-pattern>/user/*</url-pattern>
</servlet-mapping>
控制器:
@Controller
@RequestMapping(value ="/user")
public class Controller {
@RequestMapping(method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody List<User> getUsers() {
}
@RequestMapping( value = "/{id}", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody User getUser(@PathVariable int id) throws NotFoundException {
}
@RequestMapping( value = "/remove/{id}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.OK)
public void delete(@PathVariable int id) throws NotFoundException {
}
}
servlet:
当我尝试使用"/user"获取所有用户时,它可以正常工作,但是当我尝试获取特定的用户时,例如"user/75".75代表我得到的{id}:
When I try ´/user´ to get all users it workings fine but when i try to get certain user for example ´user/75´. 75 representing the {id} I'm getting:
No mapping found for HTTP request with URI [/user/75] in DispatcherServlet with name 'userService'
不确定为什么.我的网址格式错误吗?谢谢您的帮助.
not sure why. is my url-pattern wrong? thanks for any help.
推荐答案
将web.xml的< url-pattern>/user/*</url-pattern>
行更改为< url-pattern>/</url-pattern>
Change the line <url-pattern>/user/*</url-pattern>
of your web.xml to <url-pattern>/</url-pattern>
此更改使Dispatcher Servlet捕获'/'下的所有请求,而在先前的配置中,它捕获/user下的所有请求(这意味着控制器的find方法将映射到/user/user/{id}
)
This change makes the Dispatcher Servlet catch all requests under '/' whereas in your previous configuration it was catching everying under /user (meaning that your controller's find method would be mapped at /user/user/{id}
)
这篇关于找不到RequestMapping DisplatcherServlet映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!