加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:24:29 所属栏目:大数据 来源:网络整理
导读:我正在尝试访问我编写的RESTful Web服务: http://localhost:8080/dukegen/ws/family/1 但是使用浏览器中的地址栏获取404并且不知道原因.我想恢复JSON.我把杰克逊2放在我的课堂上: 这是服务器输出: Jan 14,2014 8:29:55 PM org.springframework.web.servle

我正在尝试访问我编写的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:

任何帮助将不胜感激.

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

首先,在您的请求映射中,映射应该是一致的.
你的类应该映射到“/ 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;
       }
   }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读