Spring Controller 数据传递中注解的使用

January 1, 2018 · 145 words · One minute

##根据处理Request的不同内容分为4类:

  1. 处理Request URI部分的注解:@PathVariable
  2. 处理Request Header部分的注解:@RequestHeader@CookieValue
  3. 处理Request Body部分的注解:@RequestParam@RequestBody
  4. 处理Attribute类型的注解:@SessionAttribute@ModelAttribute

##@PathVariable

  • 当使用@RequestMapping URI template样式映射时,即url/{param},这时param可以通过@PathVariable注解绑定它传过来的值到方法的参数上
    @Controller  
    public class RelativePathUriTemplateController {  
      
      @RequestMapping("/url/{param}")  
      public void getParams(@PathVariable String param) {      
        //....
      }  
    }  

##@RequestHeader

  • 可以把Request请求的Header部分的值绑定到方法的参数上
    Host                    localhost:8080  
    Accept                  text/html,application/xhtml+xml,application/xml;q=0.9  
    Accept-Language         fr,en-gb;q=0.7,en;q=0.3  
    Accept-Encoding         gzip,deflate  
    Accept-Charset          ISO-8859-1,utf-8;q=0.7,*;q=0.7  
    Keep-Alive              300  
    @RequestMapping("/url")  
    public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,  
                                  @RequestHeader("Keep-Alive") long keepAlive)  {  
      
      //...  
      
    }

request header部分的 Accept-Encoding的值,绑定到参数encoding上了, Keep-Alive header的值绑定到参数keepAlive上。


##@CookieValue

  • 可以把RequestHeader中关于cookie的值绑定到方法的参数上
@RequestMapping("/url")  
public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie)  {  
  
  //...  
  
} 

##@RequestBody

  • 通常用来处理Content-Type不是application/x-www-form-urlencoded编码的内容,例如application/jsonapplication/xml
  • 通过使用HandlerAdapter配置的HttpMessageConverter来解析data body,然后绑定到相应的Bean
  • 因为配置有FormHttpMessageConverter,所以也可以用来处理application/x-www-form-urlencoded的内容,处理完的结果放在一个MultiValueMap<String,Stirng>
    @RequestMapping(value = "/url", method = RequestMethod.POST)  
    public void handle(@RequestBody String body) throws IOException {  
      //...
    } 

##@ResponseBody

  • 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式的数据写入到Response对象的body数据区
    @ResponseBody
    @RequestMapping("/")
    public RedirectView root() {
        return new RedirectView("/index/index.html");
    }

@SessionAttributes

  • 用来绑定HttpSession中的Attribute对象的值
    @Controller  
    @RequestMapping("/editPet.do")  
    @SessionAttributes("pet")  
    public class EditPetForm {  
        // ...  
    }  

@ModelAttribute

  • 用于方法上时通常用来处理@RequestMapping之前,为请求绑定需要从后台查询的Model
    @ModelAttribute  
    public Account addAccount(@RequestParam String number) {  
        return accountManager.findAccount(number);  
    }  

这种方式实际的效果就是在调用@RequestMapping的方法之前,为request对象的modelput("account",Account)

  • 用于参数上时通过名称对应,把相应名称的值绑定到注解的参数Bean上,要绑定的值来源于
    • @SessionAttributes启用的Attribute对象
    • @ModelAttribute用于方法上时指定的Model对象
    • 以上两种情况都没有时,new一个需要绑定的Bean对象,然后把Request中按名称对应的方式把值绑定到Bean
    @RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)  
    public String processSubmit(@ModelAttribute Pet pet) {  
         
    }  

首先查询 @SessionAttributes有无绑定的Pet对象,若没有则查询@ModelAttribute方法层面上是否绑定了Pet对象,若没有则将URI template中的值按对应的名称绑定到Pet对象的各属性上