新建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;
}
}