spring boot url variable code example

Example: @pathvariable spring boot

package com.zetcode.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
  
    @RequestMapping(path="/{name}/{age}")
    public String getMessage(@PathVariable("name") String name, 
            @PathVariable("age") String age) {
        var msg = String.format("%s is %s years old", name, age);
        return msg;
    }
  
}

Tags:

Java Example