0

I need to handle requests like

www.example.com/student/thisisname?age=23&country=UK&city=London

I am just interested in thisisname part and value of city parameter.

I have following RequestMapping but it does not work. I tried {name}{.*:city} as well.

@RequestMapping(value = "/{name:.*}{city}", method = RequestMethod.GET)
Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
Daniel Newtown
  • 2,873
  • 8
  • 30
  • 64
  • 2
    `city=London` is a query parameter. Use a `@RequestParam` annotated method parameter. – Sotirios Delimanolis Jul 08 '15 at 04:20
  • You can go for @PathVariable, have a look at this URL http://stackoverflow.com/questions/19803731/spring-mvc-pathvariable – Make Jul 08 '15 at 04:23
  • Use `@PathVariable` annotated method parameter. Whenever you are passing parameter in the URL with GET method, use @PathVariable annotation to get it – Vignesh Shiv Jul 08 '15 at 04:30
  • Are you sure it makes sense to have a student's name as a path variable? Wouldn't a student ID work better? – Kayaman Jul 08 '15 at 05:02

2 Answers2

1

You can do it by 2 ways. Either using @RequestParam or @PathVariable

  1. By Using @RequestParam
@RequestMapping(value = "/name", method = RequestMethod.GET)
public void someMethod(@RequestParam String city){}
  1. By using @PathVariable
@RequestMapping(value = "/name/{city}", method = RequestMethod.GET)
public void someMethod(@PathVariable String city){}

You can use any of this method just you need to concentrate on URL

Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
1

You can handle it using PathVariable and RequestParam annotation. In below code name is thisisname part and city is query param city value.

@RequestMapping(value = "/student/{name}", method = RequestMethod.GET)
public void someMethod(@PathVariable String name, @RequestParam("city") String city){

}
Rishi Saraf
  • 1,644
  • 2
  • 14
  • 27
  • the code returns QueryParam cannot be resolved to a type – Daniel Newtown Jul 08 '15 at 07:29
  • @DanielNewtown you have to add jsr311-api dependency to get QueryParam – Rishi Saraf Jul 08 '15 at 11:10
  • `@QueryParam` is a JAX-RS annotation. They are using Spring MVC, which does not provide an implementation for JAX-RS. – Sotirios Delimanolis Jul 08 '15 at 14:31
  • @SotiriosDelimanolis actually you can use spring-mvc and JAX-RS together. Anyway I have edited my answer and replaced QueryParam with RequestParam – Rishi Saraf Jul 08 '15 at 15:13
  • No, you can use Spring with JAX-RS. Spring MVC is its own stack. – Sotirios Delimanolis Jul 08 '15 at 15:22
  • Hi, ```@RequestParam looks for params in your url e.g. www.exampl.com/student?name=myname. Where name id the param id and myname is the value of the param. @PathVariable will look for a variable in you url path e.g www.exampl.com/student/mname where myname is the value in the path. In you controller @Pathvariable woul be accessed like this @GetMapping(value="student/{name}"). Then you passt it in your method like this public Student getStudent(@Pathvariable("name") String studentName). For @RequestParam, they will be picked as long as they exist in you request url.``` – ochiWlad Aug 16 '17 at 15:48