来自SpringMVC for DispatcherServlet的REST端点的404错误

时间:2016-07-27 17:11:12

标签: spring rest spring-mvc web.xml

已解决此问题 Spring invokes wrong controller mapping Spring @Controllers URL总是相对于处理它们的Spring Dispatcher Servlet进行解释。因此,如果您将调度程序servlet映射到web.xml中的/ api / ,那么上面控制器的URL是/ api / api / choice

双字符串 service / service / 1234 正在运作。

原始邮寄

访问REST资源端点会给出404错误,尽管似乎所有内容都已正确定义:

日志输出:

DEBUG DispatcherServlet with name 'mvc-dispatcher' processing GET request for [/myapp/service/1234]
DEBUG Looking up handler method for path /1234
DEBUG Did not find handler method for [/1234]
WARN  No mapping found for HTTP request with URI [/myapp/service/1234] in DispatcherServlet with name 'mvc-dispatcher'

的web.xml

<servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/service/*</url-pattern>
    </servlet-mapping>

SpringMVC控制器

@RestController
@RequestMapping("/service")
public class RESTController {
  @RequestMapping(value="/{id}", method=RequestMethod.GET)
  public String getResult ( @PathVariable String id ) 
  {
    //... get JSON result
  }
}

预期调用:myapp/service/1234

还尝试了以下选项: 1)不要定义RequestMapping类,只需执行方法请求映射

@RequestMapping("/service/{id}")

2)以及

@RequestMapping("/service*/{id}")
@RequestMapping("/service**/{id}")

使用上面的日志继续获得404.

1 个答案:

答案 0 :(得分:1)

更新您的web.xml文件:

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