微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在简单的Spring 4 REST服务上获得404

我正在尝试访问我编写的RESTful Web服务:

http://localhost:8080/dukegen/ws/family/1

但是使用浏览器中的地址栏获取404并且不知道原因.我想恢复JSON.我把杰克逊2放在我的课堂上:

jackson-databind

这是服务器输出

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中设置的调度程序:

 dispatcherdispatcherServletaram>
            aram-name>contextConfigLocationaram-name>
            aram-value>classpath:/mvcContext.xmlaram-value>
        aram>
  servlet-mapping>
        dispatcherservlet-mapping>

我的mvcContext.xml:

spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    

任何帮助将不胜感激.

最佳答案
有些事情在这里不正确.

首先,在您的请求映射中,映射应该是一致的.
你的类应该映射到“/ ws”,产生结果的方法应该是“/ family / {familyId}”

在你的web.xml中你已经配置了servlet来响应/ ws / *并且你的控制器是请求映射到ws再次.这不会工作.

一旦你的servlet拦截了“/ ws / *”,就不应该在Request Mappings中重复它. Controller仅响应其上下文中的URL模式.无论您的URL中的“/ ws”之后是什么,只能在控制器的上下文中.

我通常更喜欢将servlet映射到“/”以及在控制器内编码的所有其他分辨率.不过只是我的偏好.

所以正确的配置是

web.xml中

    dispatcherdispatcherServletaram>
            aram-name>contextConfigLocationaram-name>
            aram-value>classpath:/mvcContext.xmlaram-value>
        aram>
    servlet-mapping>
        dispatcherservlet-mapping>

和控制器

   @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;
       }
   }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐