17

I want to pass in my parameters to my web service in the format:

http://.../greetings/neil/1

Rather than

http://.../greetings?name=neil&id=1

So I changed my code from (note, I've only included the first parameter in the code):

@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
    return new Greeting(counter.incrementAndGet(),
                        String.format(template, name));
}

to:

@RequestMapping
public Greeting greeting(@PathVariable String name) {
    return new Greeting(counter.incrementAndGet(),
                        String.format(template, name));
}

which works, however I do not know how to add default values to @PathVariable so that for example:

http://.../greetings/

http://.../greetings/neil/

would work as it does with query parameters.

How do I do this? I thought maybe it would pass null, but it just generates a page error.

I guess the answer might be to add multiple overloads, but that sounds a bit messy.

thanks.

thanks.

1

3 Answers 3

26

Spring 5 / Spring Boot 2 example:

@RequestMapping({"/greeting-blocking/{name}","/greeting-blocking/"})
public ResponseEntity<User> greetingBlocking(
        @PathVariable(name = "name", required = false) String name) {
    String username = StringUtils.isEmpty(name) ? "anonymous" : name;
    return ResponseEntity.ok().body(User.create(username));
}

@RequestMapping({"/greeting-reactive/{name}","/greeting-reactive/"})
public Mono<ResponseEntity<User>> greetingReactive(
        @PathVariable(name = "name", required = false) String name) {
    String username = StringUtils.isEmpty(name) ? "anonymous" : name;
    return userReactiveRepo.findByName(username).map(user -> ResponseEntity.ok().body(user));
}
1
  • thanks. I didn't know that I can use two URLs for the same method. It's very helpful Feb 23, 2022 at 21:26
13

How about the following way? I am using java.util.Optional class which acts as a wrapper over objects that can be null or not-null.

@RequestMapping
public Greeting greeting(@PathVariable Optional<String> name) {
    String newName = "";  
    if (name.isPresent()) {
        newName = name.get()   //returns the id
    }
    return new Greeting(counter.incrementAndGet(),
                        String.format(template, newName));
}

Alternately, you can define two separate request mapping handlers:

@RequestMapping("/greeting")
public Greeting defaultGreeting()

AND

@RequestMapping("/greeting/{name}")
public Greeting withNameGreeting(@PathVariable String name)
2
  • 1
    The second method I already did that, but wanted something like your first method. However, I can't get it to work when nothing is supplied, i.e. /greeting. Do I need to add anything to the class? the only way I can get it to do anything is by adding: @RequestMapping("/greeting/{name}") Jan 12, 2017 at 19:07
  • Are you saying that when you use @RequestMapping("/greeting/{name}") and then invoke the URL /greeting, it doesn't work? What exception does Spring throw?
    – VHS
    Jan 12, 2017 at 19:33
1

One liner

@RequestMapping
public Greeting greeting(@PathVariable Optional<String> name) {
    return new Greeting(counter.incrementAndGet(),
                        String.format(template, name.orElse("World")));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.