Spring3 REST中的@
2011-03-30 22:05
309 查看
@RequestMapping @RequestParam @PathVariable @ModelAttributes @SessionAttributes @CookieValue @RequestHeader 都是Spring MVC REST中的注释方法,这几种注释方法的用法如下: 1#Controller 示例 URL示例: POST /users/query?userId=1234 @Controller public class JavaBlogerController { @Autowired JavaBlogerService serviceLayer; @RequestMapping public String delete(@RequestParam String userId ){ serviceLayer.query (userId); return "redirect:list"; } } 2#CookieValue 示例 @RequestMapping ("/userList") public String delete(@CookieValue("JSESSIONID") String sessionId ){ } 注释表明,CookieValue方法的参数可以绑定到HTTP的Cookie。支持在Servlet和Portlet环境注明处理方法。 3#RequestHeader示例显示结果 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729) Accept-Language: en-gb,en;q=0.5 Accept-Encoding: gzip,deflate Keep-Alive: 300 @RequestMapping("/showJavaBlogerInfo") public void showHeadInfo( @RequestHeader("User-Agent"),String userAgent, @RequestHeader("Accept-Encoding"),String encoding, @RequestHeader("Keep-Alive"),long keepAlive, ){ } 4#HiddenHttpMethodFilter 示例 ·Allows HTML browsers to emulate PUT and DELETE requests HTML forms only support GET/POST natively ·Special hidden parameter determines RequestMethod 上面2句话需要翻译一下
@RequestMapping (metthod=RequestMethod.DELETE) public void delete (@RequestParam String userId) { System.out.println(userId); } 5#/** 分页信息装载 */ 示例 @RequestMapping(value = "/page/{pageNo}", method = RequestMethod.GET) public String pageLoad(@PathVariable int pageNo) { String mapping = "mainPage"; System.out.println(pageNo); return mapping; } 6#SessionAttributes 示例 @Controller @SessionAttributes( "currentUser " ) public class GreetingController { @RequestMapping public void hello(@ModelAttribute( "currentUser " ) User user) { // user.sayHello() } // } 使用@ModelAttribute 需要访问 Session 属性的 controller 上加上 @SessionAttributes,然后在 action 需要的 User 参数上加上 @ModelAttribute,并保证两者的属性名称一致。SpringMVC 就会自动将 @SessionAttributes 定义的属性注入到 ModelMap 对象,在 setup action 的参数列表时,去 ModelMap 中取到这样的对象,再添加到参数列表。只要我们不去调用 SessionStatus 的 setComplete() 方法,这个对象就会一直保留在 Session 中,从而实现 Session 信息的共享。 –end–
@RequestMapping (metthod=RequestMethod.DELETE) public void delete (@RequestParam String userId) { System.out.println(userId); } 5#/** 分页信息装载 */ 示例 @RequestMapping(value = "/page/{pageNo}", method = RequestMethod.GET) public String pageLoad(@PathVariable int pageNo) { String mapping = "mainPage"; System.out.println(pageNo); return mapping; } 6#SessionAttributes 示例 @Controller @SessionAttributes( "currentUser " ) public class GreetingController { @RequestMapping public void hello(@ModelAttribute( "currentUser " ) User user) { // user.sayHello() } // } 使用@ModelAttribute 需要访问 Session 属性的 controller 上加上 @SessionAttributes,然后在 action 需要的 User 参数上加上 @ModelAttribute,并保证两者的属性名称一致。SpringMVC 就会自动将 @SessionAttributes 定义的属性注入到 ModelMap 对象,在 setup action 的参数列表时,去 ModelMap 中取到这样的对象,再添加到参数列表。只要我们不去调用 SessionStatus 的 setComplete() 方法,这个对象就会一直保留在 Session 中,从而实现 Session 信息的共享。 –end–
相关文章推荐
- Spring RestTemplate详解
- SpringBoot + mybatis + Swagger快速构建REST API并生成优美的API文档
- 详解如何用spring Restdocs创建API文档
- (12)自定义数据流(实战Docker事件推送的REST API)——响应式Spring的道法术器
- Spring提供的用于访问Rest服务的客户端:RestTemplate实践
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
- spring boot RestTemplate 发送 get 请求
- springboot中配置restTemplate及简单使用
- spring RestTemplate用法详解
- 关于Spring RestTemplate乱码问题解决
- spring和resteasy整合的注意事项
- spring RestTemplate 实例(NameValuePair)
- Spring REST文档的编写
- spring+resteasy(JSF)搭建接口
- springboot-1-restTemplate的使用
- SpringMVC Rest 风格
- Creating a REST API with Spring Boot and MongoDB
- spring RestTemplate使用
- Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header
- Spring REST实践之REST基本介绍