我在SpringMVC中的servlet在一般情况下正常实现REST Api,但是当我尝试删除子资源时出现了一个奇怪的错误。
说我有: - 用户作为父资源 - 作为子资源的地址
如果我创建(POST),更新(PUT)或检索(GET)地址一切正常,但当我尝试删除它时,我收到404未找到并查看我看到的日志
servlet.DispatcherServlet (DispatcherServlet.java:1218) - Rendering view
[org.springframework.web.servlet.view.InternalResourceView: name 'users/544e6b02300429b462c6b8ea/billing-addresses/4'; URL [users/544e6b02300429b462c6b8ea/billing-addresses/4]] in DispatcherServlet with name 'api-dispatcher'
view.AbstractView (AbstractView.java:377) - Added model object 'id_user' of type [java.lang.String] to request in view with name 'users/544e6b02300429b462c6b8ea/billing-addresses/4'
view.AbstractView (AbstractView.java:377) - Added model object 'id_addresses' of type [java.lang.String] to request in view with name 'users/544e6b02300429b462c6b8ea/billing-addresses/4'
view.InternalResourceView (InternalResourceView.java:207) - Forwarding to resource [users/544e6b02300429b462c6b8ea/billing-addresseses/4] in InternalResourceView 'users/544e6b02300429b462c6b8ea/billing-addresses/4'
servlet.DispatcherServlet (DispatcherServlet.java:838) - DispatcherServlet with name 'api-dispatcher' processing DELETE request for [/rest-local/rest/users/544e6b02300429b462c6b8ea/billing-addresses/users/544e6b02300429b462c6b8ea/billing-addresses/4]
handler.AbstractHandlerMethodMapping (AbstractHandlerMethodMapping.java:246) - Looking up handler method for path /users/544e6b02300429b462c6b8ea/billing-addresses/users/544e6b02300429b462c6b8ea/billing-addresses/4
handler.AbstractHandlerMethodMapping (AbstractHandlerMethodMapping.java:254) - Did not find handler method for [/users/544e6b02300429b462c6b8ea/billing-addresses/users/544e6b02300429b462c6b8ea/billing-addresses/4]
servlet.DispatcherServlet (DispatcherServlet.java:1114) - No mapping found for HTTP request with URI [/rest-local/rest/users/544e6b02300429b462c6b8ea/billing-addresses/users/544e6b02300429b462c6b8ea/billing-addresses/4] in DispatcherServlet with name 'api-dispatcher'
servlet.FrameworkServlet (FrameworkServlet.java:991) - Successfully completed request
servlet.FrameworkServlet (FrameworkServlet.java:991) - Successfully completed request
出于某种原因,我不知道它在某个时刻附加了相对路径两次,当然最终路径没有被处理,我得到了404找不到。
在代码库中,这是我尝试删除子资源时唯一的情况,因为DELETE正在运行的所有现有资源,只有在这种情况下,我才会看到这种奇怪的行为(此资源的代码完全相同,对于其他人)。
服务器端映射是:
@RequestMapping(value = "/{id_user}/billing-addresses/{id_address}", method = RequestMethod.DELETE, produces = "application/json")
public ModelAndView deleteBillingAddress(@PathVariable("id_user") String idUser,
@PathVariable("id_address") String idAddress,
HttpServletResponse httpServletResponse,
Principal principal)
我正在测试它:
curl -i -X DELETE \
-H "Content-Type:application/json" \
-H "Accept-Language:it" \
'http://localhost:8081/rest-local/rest/users/544e6b02300429b462c6b8ea/billing-addresses/4'
有什么想法吗?
答案 0 :(得分:0)
问题是我返回了一个用new ModelAndView()
实例化的空ModelAndView对象。
现在我改变了它:
new ModelAndView(this.userMapperView, DATA_FIELD, billingAddress);
不确定这是最好的方法,但至少现在它有效。