具有空值的Spring MVC @Path变量 [英] Spring MVC @Path variable with null value

查看:33
本文介绍了具有空值的Spring MVC @Path变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在使用 spring boot 开发应用程序.在资源中使用路径变量(**@PathVariable** 注释)和请求参数(**@RequestParam("name")** 注释).我的代码获取请求参数值而不是路径变量值,我获取路径变量值为空.请任何人建议我解决这个问题.

@RequestMapping(value = "/api/user/{id}", method = RequestMethod.GET)public void get(@RequestParam("name") String name, @PathVariable Integer id);{System.out.println("name="+name);System.out.println("id="+id)}

网址:

Am developing an application using spring boot. In Resource am using path variable(**@PathVariable** annotation) and Request Param(**@RequestParam("name")** annotation). My code fetching the request param value but not the path variable value,I am getting the path variable value as null. Please any one suggest me to solve this issue.

@RequestMapping(value = "/api/user/{id}", method = RequestMethod.GET)  
public void get(@RequestParam("name")  String name, @PathVariable Integer id); {

        System.out.println("name="+name);
        System.out.println("id="+id)
}

URL:
http://localhost:8080/api/user/2?name=neeru

OUTPUT:
name=neeru
id=null

i also tried

**@RequestMapping(value = "/api/user/id={id}", method = RequestMethod.GET)** 

URL:
http://localhost:8080/api/user/id=2?name=neeru but getting same id value=null

i have added one more method -only has @PathVariable

@RequestMapping(path="/api/user/name/{name}", method = RequestMethod.GET)    
   void get( @PathVariable("value=name") String name){

   System.out.println("name="+name)
}

but result is same path variable value name=null

解决方案

  1. @PathVariable is to obtain some placeholder from the uri
  2. @RequestParam is to obtain an parameter

Change your endpoint like this

http://localhost:8080/api/user/2/users?name=neeru

`

@RequestMapping(value = "api/user/{id}/users", method = RequestMethod.GET)  
public void get(@RequestParam("name")  String name, @PathVariable("id") Integer id); {
        System.out.println("name="+name);
        System.out.println("id="+id)
}

`

Output:

name = neeru

id = 2

这篇关于具有空值的Spring MVC @Path变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆