package com.example.demo.controllers import com.example.demo.domain.Cat import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController @RestController class Home { @GetMapping("/") String index() { return "Return as plain text" } @GetMapping("/cat") Cat something() { return new Cat() } @GetMapping("/param") String param(@RequestParam(value = "param1", defaultValue = "param1") String param1) { return param1 } }
@RestController
to tell spring that it is a rest controller class@GetMapping(“/”)
above the method. This example mapped the root path to the method index()getter
of that Object.@RequestParam
. We can map it to the parameter with the method parameter, and give it a default value if the user do not supply one.We have all kind of mapping, and we can map the same path with different http method to a different functions.
We can add @RequestMapping(“something”)
to the class so that any mapping under this class will become /something/…