新建HelloController類
添加註解
@RestController //等同於@Controller+@ResponseBody
@RequestMapping(value = "hello") //訪問此controller類時, hello爲'父'路徑 localhost:8080/hello/hi
public class HelloController {
@GetMapping(value = {"/say/{id}","/hi/{id}"}) //localhost:8080/hello/say/123 == localhost:8080/hello/hi/123
public String say(@PathVariable(value = "id") Integer id){ //@PathVarable(value="id") 獲取restful路徑中的參數 localhost:8080/hello/say/123 id==123
return id;
}
public String say(@RequestParam(value = "id",requied=false,defaultValue="0") Integer id){ //@RequestParam 路徑訪問形式爲帶參數 localhost:8080/hello/say?id=123 requied必填 defaultValue默認值 return id; }}