zoukankan      html  css  js  c++  java
  • SpringMVC 参数绑定注解

    @PathVariable 用于访问URI模板变量。
    @GetMapping("/owners/{ownerId}/pets/{petId}")
    public String findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
        Owner owner = ownerService.findOwner(ownerId);
        Pet pet = owner.getPet(petId);
        model.addAttribute("pet", pet);
        return "displayPet";
    }
    
    @MatrixVariable 用于访问URI路径段中的名称/值对。
    // GET /pets/42;q=11;r=22
    
    @GetMapping("/pets/{petId}")
    public void findPet(@PathVariable String petId, @MatrixVariable int q) {
    
        // petId == 42
        // q == 11
    
    }
    
    // GET /owners/42;q=11;r=12/pets/21;q=22;s=23
    
    @GetMapping("/owners/{ownerId}/pets/{petId}")
    public void findPet(
            @MatrixVariable MultiValueMap<String, String> matrixVars,
            @MatrixVariable(pathVar="petId"") MultiValueMap<String, String> petMatrixVars) {
    
        // matrixVars: ["q" : [11,22], "r" : 12, "s" : 23]
        // petMatrixVars: ["q" : 11, "s" : 23]
    
    }
    
    @RequestBody 用于访问HTTP请求正文。一般用来接受json数据,将json数据转化为对象,反序列作用
    /*
    public class User {
        private String name;
        private int age;
    ...
    }
    */
    #test 1
     @PostMapping(value = "/test")
        public User testAnnotation(@Validated @RequestBody User user){
            return user;
        }
    #test 2
      @PostMapping(value = "/test")
        public String testAnnotation(@Validated @RequestBody String user){
            return user;
        }
    

    @RequestParam 用于访问Servlet请求参数,包括多部分文件。参数值将转换为声明的方法参数类型
    //Get http://localhost:8989/test?name=lisi
     @GetMapping(value = "/test")
        public String testAnnotation(@RequestParam(value="name") String name){
            return name;
        }
    
    @RequestHeader 用于访问请求标头。标头值将转换为声明的方法参数类型。
    @PostMapping(value = "/test")
        public String testAnnotation(@RequestHeader("Accept-Encoding") String encoding) {
            return encoding;
        }
    

    @CookieValue 用于访问cookie。Cookies值将转换为声明的方法参数类型。
    //JSESSIONID = 415A4AC178C59DACE0B2C9CA727CDD84
    
    @GetMapping("/demo")
    public void handle(@CookieValue("JSESSIONID") String cookie) { 
        //...
    }
    
    HttpEntity HttpEntity或多或少与使用@RequestBody相同,但它基于公开请求标头和正文的容器对象。
    @PostMapping("/accounts")
    public void handle(HttpEntity<Account> entity) {
        // ...
    }
    

    启用MultipartResolver后,将解析具有multipart / form-data的POST请求的内容,并将其作为常规请求参数进行访问。

    @Controller
    public class FileUploadController {
    
        @PostMapping("/form")
        public String handleFormUpload(@RequestParam("name") String name,
                @RequestParam("file") MultipartFile file) {
    
            if (!file.isEmpty()) {
                byte[] bytes = file.getBytes();
                // store the bytes somewhere
                return "redirect:uploadSuccess";
            }
            return "redirect:uploadFailure";
        }
    }
    

    将参数类型声明为List 允许解析相同参数名称的多个文件。
    如果将@RequestParam批注声明为Map <String,MultipartFile>或MultiValueMap <String,MultipartFile>,但未在注释中指定参数名称,则将使用每个给定参数名称的多部分文件填充该映射。

    @ModelAttribute 用于访问模型中的现有属性(如果不存在,则进行实例化),并应用数据绑定和验证。
    @PostMapping(value = "/test/name/{name}/age/{age}")
        public User testAnnotation(@ModelAttribute User user){
            return user;
        }
    

    默认情况下,任何不是简单值类型(由BeanUtils#isSimpleProperty确定 )且未被其他任何参数解析器解析的参数都将被视为@ModelAttribute。

    @SessionAttribute 与访问由于类级@SessionAttributes声明而存储在会话中的模型属性相反,用于访问任何会话属性。

    可以用来访问先前通过Servlet过滤器或HandlerInterceptor创建的设置的请求属性

    @RequestAttribute 用于访问请求属性。

    可以用来访问先前通过Servlet过滤器或HandlerInterceptor创建的设置的请求属性

    @ResponseBody 在方法上使用可以通过HttpMessageConverter将返回序列化为响应主体,将对象转为为json格式,序列化作用。
    @Controller
    public class DockerFileController {
        @RequestMapping(value = "/test")
        public User testAnnotation(@ModelAttribute User user){
            User user1 =new User();
            user1.setName("zs");
            user1.setName("zs");
            return user1;
        }
    }
    

    报错,由于未使用@ResponseBody将会使用视图解析器解析返回对象,返回视图(如jsp页面等)不存在。换成@RestController即可

    @RequestMapping参数
    name # 映射器名称
    value #映射请求路径,数组 可映射多个路径
    method #请求类型,如 GET、POST、PUT、DELETE等;进阶注解如PostMapping、GetMapping等
    consumes #
    
    @PostMapping(path = "/pets", consumes = "application/json") 
    public void addPet(@RequestBody Pet pet) {
        // ...
    }
    
    produces #指定返回的内容类型 ,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
    
    @GetMapping(path = "/pets/{petId}", produces = {"application/xml;charset=UTF-8"}) 
    @ResponseBody
    public Pet getPet(@PathVariable String petId) {
        // ...
    }
    
    params #指定request中必须包含某些参数值时该映射才匹配。
    @GetMapping(path = "/pets/{petId}", params = "myParam=myValue") 
    public void findPet(@PathVariable String petId) {
        // ...
    }
    
    header #指定request中必须包含某些指定的header值,才能让该方法处理请求。
    @GetMapping(path = "/pets", headers = "myHeader=myValue") 
    public void findPet(@PathVariable String petId) {
        // ...
    }
          
    
  • 相关阅读:
    JavaScript 23 Window
    JavaScript 22 自定义对象
    JavaScript 21 Math
    History 7 : Christianity, Science, and The Enlightenment
    History : The Atlantic Slave Trade
    History 6 : Aztec and Inca Empires / African empires 8001500
    003 几个python编程例子
    006 网络的瓶颈效应
    0212 Logistic(逻辑)回归
    002 用Python打印九九乘法表与金字塔(*)星号
  • 原文地址:https://www.cnblogs.com/jinit/p/13867129.html
Copyright © 2011-2022 走看看