Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring mvc Spring MVC Rest api处理程序中的自定义参数转换_Spring Mvc_Parameters_Handler_Parameter Passing - Fatal编程技术网

Spring mvc Spring MVC Rest api处理程序中的自定义参数转换

Spring mvc Spring MVC Rest api处理程序中的自定义参数转换,spring-mvc,parameters,handler,parameter-passing,Spring Mvc,Parameters,Handler,Parameter Passing,我们正在使用SpringWebMVC构建一个简单的RESTAPI。我们的一个处理程序将一个名为ID的int作为请求参数 @RequestMapping(value="/requestEntity", method=RequestMethod.GET) public ModelMap getEntity( @PathVariable(value="id")final Integer id, HttpServletResponse response) throws IOExc

我们正在使用SpringWebMVC构建一个简单的RESTAPI。我们的一个处理程序将一个名为ID的int作为请求参数

@RequestMapping(value="/requestEntity",
    method=RequestMethod.GET)
public ModelMap getEntity(
    @PathVariable(value="id")final Integer id,
    HttpServletResponse response) throws IOException  {
我知道请求对象的请求参数值为字符串name1=value1&name2=22,当处理程序将ID作为其参数时,它将神奇地接收一个int

"/requestEntity", id="5" -> getEntity(5);
不幸的是,如果我们将值1、2、3作为输入发送,则会得到数字123作为参数。如果我们发送5;我们得到5作为参数

"/requestEntity", id="1 2 3" -> getEntity(123); // WRONG -- we want to throw an error
"/requestEntity", id="5;123" -> getEntity(5);   // ALSO WRONG

据我所知,Spring正在进行一些转换,将字符串参数值转换为处理程序需要的int参数。我们如何实现自己的参数转换器?

这看起来像是一个与此问题类似的问题:。有两种答案/解决方案可能适合您,我先试试这个:

@RequestMapping
public void handleRequest( HttpServletRequest request ) {
    int page = ServletRequestUtils.getIntParameter(request, "page", 1);
}