Java 无法通过requestMapping处理请求
我需要处理像这样的请求Java 无法通过requestMapping处理请求,java,spring,spring-mvc,annotations,Java,Spring,Spring Mvc,Annotations,我需要处理像这样的请求 www.example.com/student/thisisname?age=23&country=UK&city=London 我只对城市参数的部分和值感兴趣 我有以下请求映射,但它不工作。我也试过{name}{.*:city} @RequestMapping(value = "/{name:.*}{city}", method = RequestMethod.GET) 你可以用两种方法来做。使用@RequestParam或@PathVariable
www.example.com/student/thisisname?age=23&country=UK&city=London
我只对城市参数的部分和值感兴趣
我有以下请求映射,但它不工作。我也试过{name}{.*:city}
@RequestMapping(value = "/{name:.*}{city}", method = RequestMethod.GET)
你可以用两种方法来做。使用@RequestParam或@PathVariable
通过使用@RequestParam
通过使用@PathVariable
您可以使用此方法中的任何一种,只需关注URL即可。您可以使用PathVariable和RequestParam注释处理此方法。下面的代码名是thisisname部分,城市是查询参数城市值
@RequestMapping(value = "/student/{name}", method = RequestMethod.GET)
public void someMethod(@PathVariable String name, @RequestParam("city") String city){
}
city=London
是一个查询参数。使用@RequestParam
带注释的方法参数。您可以选择@PathVariable,查看此URL使用@PathVariable
带注释的方法参数。每当您使用GET方法在URL中传递参数时,请使用@PathVariable annotation来获取它您确定将学生的姓名作为路径变量有意义吗?学生ID不是更好吗?代码返回QueryParam无法解析为type@DanielNewtown您必须添加jsr311 api依赖项才能获得QueryParam@QueryParam
是一个JAX-RS注释。他们使用的是SpringMVC,它没有为JAX-RS提供实现。@SotiriosDelimanolis实际上您可以同时使用SpringMVC和JAX-RS。无论如何,我已经编辑了我的答案,并用RequestParamNo替换了QueryParam,您可以将Spring与JAX-RS一起使用。SpringMVC是它自己的堆栈。
@RequestMapping(value = "/name/{city}", method = RequestMethod.GET)
public void someMethod(@PathVariable String city){}
@RequestMapping(value = "/student/{name}", method = RequestMethod.GET)
public void someMethod(@PathVariable String name, @RequestParam("city") String city){
}