spring学习(二十九)--参数绑定注解

package springAnnotions;

import java.util.Map;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @RestController注解相当于@ResponseBody + @Controller合在一起的作用。
 * 1) 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,
 *    或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
 * 2) 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
 *    如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
 * @author qiaozhong
 */
@RestController

/**
 * RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
 * RequestMapping注解有六个属性
 * 1、 value:指定请求的实际地址,指定的地址可以是URI Template 模式;
 *    当之设置value一个属性时,value可以省略不写,当有其他属性时则需要加上进行区分。
 * 2、method: 指定请求的method类型, GET、POST、PUT、DELETE等。
 * 3、consumes:指定处理请求的提交内容类型(Content-Type),例如application/json, text/html。
 * 4、produces:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回。
 * 5、params:指定request中必须包含某些参数值是,才让该方法处理。
 * 6、headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。
 * @author qiaozhong
 */
@RequestMapping(value="/pathVariableAnnotion")
public class PathVariableAnnotion {
    
    /**
     * 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
     * 使用时机:返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;
     */
    @ResponseBody
    @RequestMapping(value="/getRequestHeader")
    /**
     * @RequestHeader绑定请求头参数到入参
     * value为key,required为是否必须存在,defaultValue为header不存在时候的默认值
     */
    public String getRequestHeader(@RequestHeader(value="Accept-Encoding", required=false, defaultValue="noSession") String encoding, 
            @RequestHeader("host") String host, @RequestHeader("Content-Type") String contentType){
        System.out.println(encoding);
        System.out.println(host);
        System.out.println(contentType);
        return encoding + host + contentType;
    }
    
    
    /**
     * @CookieValue绑定请求头cookie中的JSESSIONID到入参
     * value为key,required为是否必须存在,defaultValue为cookie不存在时候的默认值
     * @param encoding
     * @param host
     * @param contentType
     * @return
     */
    @RequestMapping(value="/getRequestCookie")
//    @ResponseBody
    public String getRequestCookie(
            @CookieValue(value="JSESSIONID", required=false, defaultValue="noSession") String sessionId){
        System.out.println(sessionId);
        return sessionId;
    }
    
    /**
     * @RequestParam绑定Content-Type:为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST
     * 常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况
     * value为key,required为是否必须存在,defaultValue为key不存在时候的默认值
     * @param id
     * @param name
     * @return
     */
    @RequestMapping(value="/getRequestParam")
//    @ResponseBody
    public String getRequestParam(@RequestParam int id, 
            @RequestParam(value="name", required=false, defaultValue="littleBall") String name){
        System.out.println(String.valueOf(id) + name);
        return String.valueOf(id) + name;
    }
    
    /**
     * @RequestBody绑定Content-Type不为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST
     * 作用:1、该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上;
     * 2、再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。
     * 
     * 使用时机:A) GET、POST方式提时, 根据request header Content-Type的值来判断:
     * application/x-www-form-urlencoded, 可选@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理
     * multipart/form-data, 使用@RequestBody不能处理
     * 其他格式, 其他包括application/json, application/xml等格式,必须使用@RequestBody来处理
     * 
     * B) PUT方式提交时, 根据Content-Type的值来判断:
     * application/x-www-form-urlencoded, 必须用使用@RequestBody来处理
     * multipart/form-data, 不能处理使用@RequestBody来处理
     * 其他格式, 必须使用@RequestBody来处理
     * 
     * 说明:request的body部分的数据编码格式由header部分的Content-Type指定;
     */
    @RequestMapping(value="/getRequestBody", method=RequestMethod.POST)
//    @ResponseBody
    public String getRequestBody(@RequestBody Map people){
        System.out.println(people.get("sex"));
        System.out.println(people.get("name"));
        return String.valueOf(people.get("sex"))+ String.valueOf(people.get("name"));
    }
}

猜你喜欢

转载自www.cnblogs.com/gllegolas/p/11817251.html