我正在尝试访问我编写的RESTful Web服务:
http://localhost:8080/dukegen/ws/family/1
但是使用浏览器中的地址栏获取404并且不知道原因.我想恢复JSON.我把杰克逊2放在我的课堂上:
这是服务器输出:
Jan 14,2014 8:29:55 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/ws/family/{familyId}] onto handler 'familyResource'
Jan 14,2014 8:29:55 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/ws/family/{familyId}.*] onto handler 'familyResource'
Jan 14,2014 8:29:55 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/ws/family/{familyId}/] onto handler 'familyResource'
Jan 14,2014 8:29:55 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 360 ms
Jan 14,2014 8:29:55 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/dukegen/ws/family/1] in DispatcherServlet with name 'dispatcher'
这是我的控制器:
@Controller
@RequestMapping("ws")
public class FamilyResource {
@RequestMapping(value="family/{familyId}",method = RequestMethod.GET,produces="application/json")
public @ResponseBody Family getFamily(@PathVariable long familyId) {
.... builds Family object ....
return family;
}
}
这是我在web.xml中设置的调度程序:
我的mvcContext.xml:
任何帮助将不胜感激.
最佳答案
有些事情在这里不正确.
原文链接:https://www.f2er.com/spring/431945.html首先,在您的请求映射中,映射应该是一致的.
你的类应该映射到“/ ws”,产生结果的方法应该是“/ family / {familyId}”
在你的web.xml中你已经配置了servlet来响应/ ws / *并且你的控制器是请求映射到ws再次.这不会工作.
一旦你的servlet拦截了“/ ws / *”,就不应该在Request Mappings中重复它. Controller仅响应其上下文中的URL模式.无论您的URL中的“/ ws”之后是什么,只能在控制器的上下文中.
我通常更喜欢将servlet映射到“/”以及在控制器内编码的所有其他分辨率.不过只是我的偏好.
所以正确的配置是
web.xml中
和控制器
@Controller
@RequestMapping("/ws")
public class FamilyResource {
@RequestMapping(value="/family/{familyId}",produces="application/json")
public @ResponseBody Family getFamily(@PathVariable long familyId) {
.... builds Family object ....
return family;
}
}