1

How can I simply redirect a url if a specific query parameter is missing?

@RestController
public class PersonController {
    //only in case the "sort" query parameter is missing
    @GetMapping("/persons")
    public String unsorted() {
         return "redirect:/persons?sort=name";
    }

    //only in case the "sort" query parameter exists
    @GetMapping("/persons")
    public String sorted() {
         //...
    }
}

5 Answers 5

2
  • Use @RequestParam to extract query parameters
  • Add parameter for @RequestParam: value, defaultValue, required

with java >= 8:

@RestController
public class PersonController {
     @GetMapping("/persons")
     public String personList(@RequestParam(value = "sort", defaultValue = "name") Optional<String> sort) {
         //handling process here
     }
}

with java < 8:

@RestController
public class PersonController {
     @GetMapping("/persons")
     public String personList(@RequestParam(value = "sort", defaultValue = "name", required=false) String sort) {
         //handling process here
     }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You could use @GetMapping.params

@GetMapping(value = "/persons", params = "sort")
public String sorted() {

Comments

1

You can use the params element. One mapping will supports params="sort" for when the sort parameter is present and the other params="!sort" for when it is missing.

However, you may want to consider using a default value instead of performing a redirect. What benefit does the redirect provide? It will require the server respond and then and have the client make a second HTTP request.

Using params

@RestController
public class PersonController {
    //only in case the "sort" query parameter is missing
    @GetMapping(value = "/persons", params = "!sort")
    public String unsorted() {
         return "redirect:/persons?sort=name";
    }

    //only in case the "sort" query parameter exists
    @GetMapping(value = "/persons", params = "sort")
    public String sorted() {
         //...
    }
}

Using default value

@RestController
public class PersonController {

    //only in case the "sort" query parameter exists
    @GetMapping("/persons")
    public String sorted(
        @RequestParam(name = "sort", defaultValue = "name") String sort)
    {
         //...
    }
}

Comments

0

You can set a default value:

@RestController
public class PersonController {
    //only in case the "sort" query parameter is missing
    @GetMapping("/persons")
    public String unsorted(@RequestParam(value = "sort", defaultValue = "name") String name) {
        // do logic
    }
}

Comments

0

You can also set the default value for the missing value and continue forward

@RequestParam(value = "sort", defaultValue = "name") String name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.