我的DispatcherServlet无法正常运行。
<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}:
No mapping found for HTTP request with URI [/user/75] in DispatcherServlet with name 'userService'
不确定原因。我的网址模式错了吗?谢谢你的帮助。
答案 0 :(得分:2)
将您的web.xml的行<url-pattern>/user/*</url-pattern>
更改为<url-pattern>/</url-pattern>
此更改使Dispatcher Servlet捕获'/'下的所有请求,而在之前的配置中,它捕获/ user下的每个请求(意味着您的控制器的find方法将映射到/user/user/{id}
)
答案 1 :(得分:2)
您目前在web.xml
和controller
中的实际情况是如此:
如果您已在root目录中部署了web应用程序(即您的webapp位于tomcat的&#39; /&#39;目录中),那么就可以访问所有用户
http://localhost:8080/user/user
并找到您的特定用户,您必须转到
http://localhost:8080/user/user/{id}
http://localhost:8080/user/user/75 //in your example
如果您想为REST服务使用以下格式
http://localhost:8080/user/
http://localhost:8080/user/{id}
然后,您必须将servlet映射更改为以下内容:
<servlet-mapping>
<servlet-name>userService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>