Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 spring mvc:在添加第三个path变量时未找到资源_Java_Rest_Spring Mvc_Path Variables_Postman - Fatal编程技术网

Java spring mvc:在添加第三个path变量时未找到资源

Java spring mvc:在添加第三个path变量时未找到资源,java,rest,spring-mvc,path-variables,postman,Java,Rest,Spring Mvc,Path Variables,Postman,我正在为我的REST服务器使用SpringMVC。我的pom.xml中的spring.version是3.2.1.0版本 我已经创建了许多RESTful API,并广泛使用了PathVariables。它很好用 但它似乎打破了以下情景。如果我有如下内容,我的REST请求找不到资源 @Controller @RequestMapping(value = { "/resourceA/{resourceAId}/resourceB/{resourceBId}/resourceC/{resourceCI

我正在为我的REST服务器使用SpringMVC。我的pom.xml中的spring.version是3.2.1.0版本

我已经创建了许多RESTful API,并广泛使用了PathVariables。它很好用

但它似乎打破了以下情景。如果我有如下内容,我的REST请求找不到资源

@Controller
@RequestMapping(value = { "/resourceA/{resourceAId}/resourceB/{resourceBId}/resourceC/{resourceCId}" })
public class TenderController {

    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public Tender capture(
        @PathVariable long resourceAId,
        @PathVariable long resourceBId,
        @PathVariable long resourceCId,
        @RequestBody Map<String, Object> requestBody) {

        ...
    }
}
但是,如果我从Java代码中删除{resourceCId},并相应地调整REST请求,它会成功地找到资源:

修订的Java代码:

@RequestMapping(value = { "/resourceA/{resourceAId}/resourceB/{resourceBId}/resourceC" })
新的(成功的)休息请求:

POST /resourceA/1/resourceB/2/resourceC HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache

{ "bodyParam1": 400, "bodyParam2": 0 }
所以基本上只要我有3个路径变量,事情就好像分崩离析了。对这里可能发生的事情有什么想法吗?我是否遇到了一个Spring MVC错误?我猜不会,因为3个路径变量应该是一个非常常见的场景(几乎不符合拐角情况)

更新: 这似乎是我的HTTP客户端(chrome postman)的问题,而不是我的服务器代码。当我通过curl发送相同的请求时,我能够得到预期的结果

更新: 实际上,无论客户机(邮递员、curl等)如何,错误都会回来并发生。所以这肯定是服务器端的问题。这是日志

01:35:39.409 [http-bio-8080-exec-5] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'mvc-dispatcher' processing POST request for [/resourceA/1/resourceB/1/resourceC/1]
01:35:39.411 [http-bio-8080-exec-5] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /resourceA/1/resourceB/1/resourceC/1
01:35:39.414 [http-bio-8080-exec-5] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public void com.sample.controller.DefaultController.unmappedRequest()]
01:35:39.414 [http-bio-8080-exec-5] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'defaultController'
01:35:39.422 [http-bio-8080-exec-5] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public void com.sample.controller.DefaultController.unmappedRequest()]: com.sample.exception.APIException: Url pattern is invalid.
01:35:39.423 [http-bio-8080-exec-5] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'globalControllerExceptionHandler'
01:35:39.423 [http-bio-8080-exec-5] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Invoking @ExceptionHandler method: public com.sample.model.ErrorInfo com.sample.controller.GlobalControllerExceptionHandler.handleAPIException(com.sample.exception.APIException)
01:35:39.460 [http-bio-8080-exec-5] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Written [com.sample.model.ErrorInfo@df27cd5] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@2ae18b1a]
01:35:39.460 [http-bio-8080-exec-5] DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'mvc-dispatcher': assuming HandlerAdapter completed request handling
01:35:39.460 [http-bio-8080-exec-5] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
找到了导致问题的原因:
所以在处理了很多不同的事情之后,我发现了导致这个问题的原因。我有一个DefaultController.java,它旨在捕获与任何其他控制器都不匹配的所有URL,并报告在我的REST服务的错误响应中找不到的好资源。DefaultController具有以下代码:

package com.sample.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sample.exception.APIException;
import com.sample.exception.APIException.Code;

@Controller
public class DefaultController {

    @RequestMapping("/**")
    public void unmappedRequest() {
        throw new APIException(Code.INVALID_URL_PATTERN,
            "There is no resource for this path");
    }
}

这对我很有效。但在本例中,不知何故,这个DefaultController“/**”在实际设置为接收“/resourceA/{resourceAId}/resourceB/{resourceBId}/resourceC/{resourceCId}”的控制器之前选择了我的Url。删除DefaultController为我修复了这个问题。现在我的问题是,如何保留DefaultController的功能,而不让它在我的TenderController之前被触发。

我相信这可能是一个Spring bug,可能会在某个时候被修复。遇到了完全相同的问题。在我们的例子中,我们可以通过将处理未映射请求的默认Spring控制器替换为处理404s的普通servlet来解决这一问题—有关如何处理404s的详细信息。

显示一个示例URL,该URL给出了404,但您希望通过此方法处理。添加了REST请求(失败的和成功的都有)。只是为了好玩,删除
value=“”
来自方法上的RequestMapping。这不应该有什么区别,但并不总是意味着它不会。您的日志有什么说明吗?响应是否只包含404或消息?只是在我的问题中添加了一个更新-当来自chrome postman的请求时,结果是出现了一些问题。如果我从curl发送相同的请求,我得到了预期的结果。欢迎使用stackoverflow,请将链接的关键点添加到您的帖子中。
package com.sample.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sample.exception.APIException;
import com.sample.exception.APIException.Code;

@Controller
public class DefaultController {

    @RequestMapping("/**")
    public void unmappedRequest() {
        throw new APIException(Code.INVALID_URL_PATTERN,
            "There is no resource for this path");
    }
}