在简单的 Spring 4 REST 服务上获取 404 [英] Getting 404 on simple Spring 4 REST service

查看:45
本文介绍了在简单的 Spring 4 REST 服务上获取 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是在浏览器中使用地址栏得到 404 并且不知道为什么.我正在尝试返回 JSON.我已将 Jackson 2 放在我的类路径中:

<依赖><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.3.1</version></依赖>

这是服务器输出:

Jan 14, 2014 8:29:55 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler信息:将 URL 路径 [/ws/family/{familyId}] 映射到处理程序familyResource"2014 年 1 月 14 日晚上 8:29:55 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler信息:将 URL 路径 [/ws/family/{familyId}.*] 映射到处理程序familyResource"2014 年 1 月 14 日晚上 8:29:55 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler信息:将 URL 路径 [/ws/family/{familyId}/] 映射到处理程序familyResource"2014 年 1 月 14 日晚上 8:29:55 org.springframework.web.servlet.FrameworkServlet initServletBean信息:FrameworkServlet 'dispatcher':初始化在 360 毫秒内完成2014 年 1 月 14 日晚上 8:29:55 org.springframework.web.servlet.DispatcherServlet noHandlerFound警告:在名称为dispatcher"的 DispatcherServlet 中,未找到具有 URI [/dukegen/ws/family/1] 的 HTTP 请求的映射

这是我的控制器:

@Controller@RequestMapping("ws")公共类家庭资源{@RequestMapping(value="family/{familyId}", method = RequestMethod.GET, products="application/json")public @ResponseBody Family getFamily(@PathVariable long familyId) {.... 构建 Family 对象 ....回归家庭;}}

这是我在 web.xml 中设置的调度程序:

 <servlet-name>调度程序</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:/mvcContext.xml</param-value></init-param><servlet-mapping><servlet-name>调度程序</servlet-name><url-pattern>/ws/*</url-pattern></servlet-mapping>

我的 mvcContext.xml:

<context:component-scan base-package="ws.hamacher.dukegen.resource"/></豆类>

任何帮助将不胜感激.

解决方案

这里有几处不正确.

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

在您的 web.xml 中,您已将 servlet 配置为响应 /ws/* 并且您的控制器再次请求映射到 ws.这行不通.

一旦 "/ws/*" 被您的 servlet 拦截,它不应在请求映射中重复.控制器仅响应其上下文中的 URL 模式.网址中 "/ws" 之后的内容仅在控制器的上下文中.

我通常更喜欢将 servlet 映射到 "/" 以及在控制器中编码的所有进一步解析.不过,这只是我的偏好.

所以正确的配置是

web.xml

 <servlet-name>调度程序</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:/mvcContext.xml</param-value></init-param><servlet-mapping><servlet-name>调度程序</servlet-name><url-pattern>/</url-pattern></servlet-mapping>

和控制器

 @Controller@RequestMapping("/ws")公共类家庭资源{@RequestMapping(value="/family/{familyId}", method = RequestMethod.GET, products="application/json")public @ResponseBody Family getFamily(@PathVariable long familyId) {.... 构建 Family 对象 ....回归家庭;}}

I am trying to access a RESTful web service I have written:

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

but getting a 404 using the address bar in the browser and do not know why. I am trying to return JSON. I have put Jackson 2 on my classpath:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.3.1</version>
</dependency>

Here is the server output:

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'

Here is my Controller:

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

}

Here is my dispatcher set up in web.xml:

 <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/mvcContext.xml</param-value>
        </init-param>
  </servlet>

  <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>

My mvcContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="ws.hamacher.dukegen.resource"/>

</beans>

Any help would appreciated.

解决方案

Couple of things are not correct here.

First, in your request mapping, the mapping should be consistent. Your class should be mapped to "/ws" and your method which produces the result should be "/family/{familyId}"

In your web.xml you have configured the servlet to respond to /ws/* and your controller is Request Mapped to ws again.This wont work.

Once "/ws/*" is intercepted by your servlet, it should not be repeated in the Request Mappings. The Controller responds to only the URL pattern within its context. Whatever is after "/ws" in your URL is only in the context of the controller.

I generally prefer the servlet to be mapped to "/" and all further resolutions coded within the controller. Just my preference, though.

So the correct configuration is

web.xml

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/mvcContext.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

and the controller

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

这篇关于在简单的 Spring 4 REST 服务上获取 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆