我们已经看过 春季MVC你好世界示例。 @RequestMapping是在Spring MVC中使用的重要注释之一。
春季MVC教程:
- 春季MVC你好世界示例
- 春季MVC Hibernate MySQL示例
- 春季MVC拦截器示例
- 春季MVC angularjs示例
- 春季MVC @RequestMapping示例
- 弹簧 组件,服务,存储库和控制器示例
- 春季MVC @ModelAttribute注释示例
- 春季MVC @RestController注释示例
- 弹簧 MultiActionController示例
- 春季MVC模型图春季MVC文件上传示例
- 春季休息ful Web服务示例
- 春季休息ful Web Service JSON示例
- 春季休息ful Web服务CRUD示例
- 春季安全 Hello World示例
- 春季安全自定义登录表单示例
@RequestMapping用于定义Web请求到处理程序方法或类的映射。 @RequestMapping可以在方法级别或类级别使用。我们稍后将看到它的示例。
如果在类级别使用@RequestMapping批注,它将用于方法级别路径的相对路径。
让我们借助示例来理解:
1 2 3 4 5 6 7 8 9 10 11 12 |
@RestController @请求映射( 值 = “ / countryController”) 上市 类 国家 Controller { @请求映射( 值 = “ /国家”, 方法 = 请求方法. 得到 , 标头 = “ Accept = application / json”) 上市 清单 getCountries() { 清单 listOfCountries = 国家 Service.getAllCountries(); 返回 listOfCountries; } |
因此网址为http:// localhost:8080 / ProjectName / countryController / countries的Web请求将执行getCountries()方法。下图将使您清楚:
方法级别的RequestMapping示例:
1 2 3 4 5 6 7 8 9 10 11 |
@RestController 上市 类 国家 Controller { @请求映射( 值 = “ /国家”, 方法 = 请求方法. 得到 , 标头 = “ Accept = application / json”) 上市 清单 getCountries() { 清单 listOfCountries = 国家 Service.getAllCountries(); 返回 listOfCountries; } |
因此网址为http:// localhost:8080 / ProjectName / countries的Web请求将执行getCountries()方法。
类级别的RequestMapping示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
@RestController @请求映射( 值 = “ /国家”) 上市 类 国家 Controller { @请求映射( 方法 = 请求方法. 开机自检 , 标头 = “ Accept = application / json”) 上市 国家 addCountry(@RequestBody 国家 国家 ) { 返回 国家 Service.addCountry( 国家 ); } @请求映射( 方法 = 请求方法. 放 , 标头 = “ Accept = application / json”) 上市 国家 updateCountry(@RequestBody 国家 国家 ) { 返回 国家 Service.updateCountry( 国家 ); } } |
如果您注意到,我们没有在此处定义方法级别的请求映射,因此在您点击时它将自动调用相应的PUT或POST方法:
http:// localhost:8080 / ProjectName /国家 如果您想为@RequestMapping注释练习更多示例,可以通过 春季休息 CRUD示例。肯定会帮助您更好地了解@RequestMapping注释。