Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我在尝试访问rest时出现404错误?_Java_Spring_Rest_Spring Mvc - Fatal编程技术网

Java 为什么我在尝试访问rest时出现404错误?

Java 为什么我在尝试访问rest时出现404错误?,java,spring,rest,spring-mvc,Java,Spring,Rest,Spring Mvc,当我尝试访问url时,我收到404错误,如: GET: /test/{id} 注:id是一个字符串,根据用户过滤器的不同而变化 我已经在web.xml中映射了“/test”,但我不知道如何声明一些“我不知道”的值,在本例中是“id” 让我举一个例子: @RequestMapping(value="/test/{id}", method = RequestMethod.GET) public ModelAndView testingId(@PathVariable String id ){

当我尝试访问url时,我收到404错误,如:

GET: /test/{id}
注:id是一个字符串,根据用户过滤器的不同而变化

我已经在web.xml中映射了“/test”,但我不知道如何声明一些“我不知道”的值,在本例中是“id”

让我举一个例子:

@RequestMapping(value="/test/{id}", method = RequestMethod.GET) 
public ModelAndView testingId(@PathVariable String id ){

    System.out.println("id");

    return new ModelAndView("returns", "returns", id);
}

有什么帮助吗?

您使用什么方法来配置web应用程序DispatcherServlet(web.xml或WebApplicationInitializer)

您的请求需要包括servlet上下文、servlet映射和资源映射。假设您的应用程序名为sample.war,并且DispatcherServlet映射到/app/*那么您的请求应该是

获取:/sample/app/test/4

  • 您正在返回ModelAndView,您的ViewResolver是什么?默认情况下,它将尝试查找具有该名称的jsp,但找不到它。您想返回jsp还是json对象
  • 您的应用程序是否使用SpringBoot,或者您是否使用@AutoScan或配置的控制器映射对其进行了配置
  • 放一条调试语句,确保您的请求进入方法,调试到spring方法,您可以看到spring如何解析视图
  • Id将是您试图查询的资源的标识符。示例:/users/123456-用户id

这是我的web.xml:web.xml/test/test/*并且ir不工作……您不需要在web.xml中映射/测试。使用/代替。如果在web.xml中使用/test,则完整url将变为GET:/sample/test/test/4无论我做什么,都会得到错误:在名为“test”的DispatcherServlet中找不到URI为[/test/abc]的HTTP请求的映射。字符串“abc”是随机的,取决于用户过滤器,我无法映射它。我该怎么办?注:我已经使用/没有更多地图,错误仍然存在。