相关注解:
@RestController (Spring4以后新加的注解,等同于 @ResponseBody + @Controller)
@RequestMapping(value=” ” method = RequestMethod.GET/POST)
url映射,其实value中有几种映射方式:
1、value=”hello/{id}” 或 value=”{id}/hello” 这种是restful方式,参数的接收方式为:
@PathVariable(“id”) Integer myId
@RequestMapping(value=" hello/{id}" method = RequestMethod.GET)
public String say(@PathVariable("id") Integer myId){
return "id:"+myId;
}
2、?id=111这种方式的写法,用@RequestParam接收值
@RequestMapping(value=" hello" method = RequestMethod.GET)
public String say(@RequestParam(value="id",required=false,defaultValue="2018") Integer myId){
return "id:"+myId;
}
注:required表示是否必传,defaultValue表示指定默认值,当没有传值的时候,使用默认值