-1

I need to start from this url, http://localhost:8080/home/filter?projectId=1;fileId=1

And I create this controller :

@GetMapping("/home/filter/{projectId}/{fileId}")
public String filter(@PathVariable("projectId") int projectId, @PathVariable("fileId") int fileId) {

    System.out.println("Project Id " + projectId);

    System.out.println("File Id " + fileId);

    return "redirect:/home";
}

When I test with : http://localhost:8080/home/filter?projectId=1;fileId=1 I recive this error :

 This application has no explicit mapping for /error, so you are seeing this as a fallback.
 Wed Apr 04 17:24:51 EEST 2018
 There was an unexpected error (type=Not Found, status=404).
 /home/filter

I don't know what to do..

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
abc
  • 494
  • 1
  • 8
  • 27
  • If you are using spring boot then definitely you are doing one of this please check here.. https://stackoverflow.com/questions/49375500/http-404-in-spring-boot-while-trying-to-access-resource-url/49376032#49376032 – Sagar Kharab Apr 04 '18 at 14:31
  • hint: `/home/filter?projectId=1;fileId=1` format is different from `/home/filter/{projectId}/{fileId}` – jhamon Apr 04 '18 at 14:31
  • It need to be like this : /home/filter{projectId}{fileId} ?? – abc Apr 04 '18 at 14:33

2 Answers2

3

You need to understand the difference between query parameters and path parameters in a URL.

  • Query parameters are those after the ? and are formed as name=value (and separated by & if there are more than 1 parameter),
    like in http://localhost:8080/home/filter?projectId=1&fileId=1.
  • Path parameters are delimited by / (and before ? if there is any),
    like in http://localhost:8080/home/filter/1/1.

For query parameters you use the @RequestParam annotation in your controller.
Example: For URLs like http://localhost:8080/home/filter?projectId=1&fileId=1 your controller can look like this:

@GetMapping("/home/filter")
public String filter(@RequestParam("projectId") int projectId,  
                     @RequestParam("fileId") int fileId) {
    ...
}

For path parameters you use the @PathVariable annotation in the controller.
Example: For URLs like http://localhost:8080/home/filter/1/1 the controller can look like this:

@GetMapping("/home/filter/{projectId}/{fileId}")
public String filter(@PathVariable("projectId") int projectId,  
                     @PathVariable("fileId") int fileId) {
    ...
}
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
  • Mind the difference between `http://localhost:8080/home/filter?projectId=1;fileId=1` and `http://localhost:8080/home/filter?projectId=1&fileId=1` – larsgrefer Apr 04 '18 at 15:12
  • 1
    @larsgrefer Thanks for pointing out. Seems I confused query and matrix parameters. Updated my answer. – Thomas Fritsch Apr 04 '18 at 15:24
  • Im just not sure if Spring implements this recommendation: https://www.w3.org/TR/1999/REC-html401-19991224/appendix/notes.html#h-B.2.2 – larsgrefer Apr 04 '18 at 15:28
  • 1
    @larsgrefer I've tested to see if Spring implements this recommendation. And it seems Spring separates the query parameters by `&` only, but not by `;`. – Thomas Fritsch Apr 05 '18 at 15:07
2

TL;DR: Just call the correct URL: http://localhost:8080/home/filter/1/1

Mind the difference between path parameters and query parameters.

Your controller mapping uses path parameters, while the URL you are calling uses query parameters

larsgrefer
  • 2,735
  • 19
  • 36
  • If i try like this, it works, but I don't understand what is the difference between http://localhost:8080/home/filter/1/1 and http://localhost:8080/home/filter?projectId=1;fileId=1 – abc Apr 04 '18 at 14:38
  • The first URL has the path `/home/filter/1/1` and no query parameters. the second URL has the path `/home/filter` and one query parameter named `projectId` with the value `1;fileId=1` – larsgrefer Apr 04 '18 at 14:42