Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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试图将URL的最后一部分作为一个长参数进行解析_Java_Spring_Rest - Fatal编程技术网

Java Spring试图将URL的最后一部分作为一个长参数进行解析

Java Spring试图将URL的最后一部分作为一个长参数进行解析,java,spring,rest,Java,Spring,Rest,当我尝试在web浏览器中通过此URL调用我的服务器时: http://localhost:8080/server_platform/api/predios/count 服务器工作正常并返回预期结果 REST控制器的定义如下: @Controller @RequestMapping(value = "api/predios") public class PredioController { @RequestMapping(value = "/count", method = { Req

当我尝试在web浏览器中通过此URL调用我的服务器时:

http://localhost:8080/server_platform/api/predios/count
服务器工作正常并返回预期结果

REST控制器的定义如下:

@Controller
@RequestMapping(value = "api/predios")
public class PredioController {

    @RequestMapping(value = "/count", method = { RequestMethod.GET }, produces = { "text/plain" })
    @ResponseStatus(value = HttpStatus.OK)
    public @ResponseBody String count(
            HttpServletResponse response) throws Exception {
        // code removed to simplify
    }    
}
但是当我使用Spring的
RestTemplate
exchange
方法时,如下所示:

restTemplate.exchange(
    url,
    HttpMethod.GET,
    entity,
    String.class);
服务器未输入count()方法并打印此错误:

org.springframework.beans.typemischException:未能将类型为“java.lang.String”的值转换为所需的类型“java.lang.Long”;嵌套异常为java.lang.NumberFormatException:对于输入字符串:“count”

变量
url
与之前在web浏览器中使用的变量完全相同。变量实体的格式也正确(事实上,如果我更改
url
删除
/count
部分,服务器将使用适当的REST控制器正确响应)

看起来Spring正试图将URL的
count
部分解析为一个长值,我找不到位置和原因

你知道为什么会这样吗

当我启动服务器时,日志包含这一行(因为它可能是一个有价值的信息):

2015-05-21 15:26:47637信息[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping]-将“{[/api/predios/count],methods=[GET],params=[],headers=[],consumes=[],products=[text/plain],custom=[])映射到公共java.lang.String es.controllers.restful.PredioController.count(javax.servlet.http.HttpServletResponse)抛出java.lang.Exception


您必须使用标题“Accept text/plain”配置REST模板。默认情况下,模板仅使用“application/json”,它不会映射到您的控制器。

您对
的理解是什么?事实上,如果我更改url删除/count部分,服务器将使用适当的REST控制器正确响应。
?对于
api/predios
,您是否有其他RequestMapping,可能有RequestParams?您是否以某种方式在rest模板中配置了exchange调用接受text/plain作为内容类型?您是第一眼看到的。伟大的rest模板有一个接受application/json而不是text/plain的头。非常感谢你。如果你给我一个答复,我会接受的。