SpringMVC 4 1使用ResponseBodyAdvice支持jsonp

本文详细介绍了SpringMVC 4.1中ResponseBodyAdvice接口的作用,特别是如何利用它实现JSONP的支持。通过分析AbstractJsonpResponseBodyAdvice的beforeBodyWrite方法,展示了如何自定义JSONP回调函数名称,并给出了一个具体的JsonpAdvice示例,以及如何在Controller中应用JSONP。最后,通过代码调试揭示了JSONP响应数据的生成过程。
摘要由CSDN通过智能技术生成
               

ResponseBodyAdvice是一个接口,接口描述,

  1. package org.springframework.web.servlet.mvc.method.annotation;  
  2.   
  3. /** 
  4.  * Allows customizing the response after the execution of an {@code @ResponseBody} 
  5.  * or an {@code ResponseEntity} controller method but before the body is written 
  6.  * with an {@code HttpMessageConverter}. 
  7.  * 
  8.  * <p>Implementations may be may be registered directly with 
  9.  * {@code RequestMappingHandlerAdapter} and {@code ExceptionHandlerExceptionResolver} 
  10.  * or more likely annotated with {@code @ControllerAdvice} in which case they 
  11.  * will be auto-detected by both. 
  12.  * 
  13.  * @author Rossen Stoyanchev 
  14.  * @since 4.1 
  15.  */  
  16. public interface ResponseBodyAdvice<T> {  
  17.   
  18.    /** 
  19.     * Whether this component supports the given controller method return type 
  20.     * and the selected {@code HttpMessageConverter} type. 
  21.     * @param returnType the return type 
  22.     * @param converterType the selected converter type 
  23.     * @return {@code true} if {@link #beforeBodyWrite} should be invoked, {@code false} otherwise 
  24.     */  
  25.    boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType);  
  26.   
  27.    /** 
  28.     * Invoked after an {@code HttpMessageConverter} is selected and just before 
  29.     * its write method is invoked. 
  30.     * @param body the body to be written 
  31.     * @param returnType the return type of the controller method 
  32.     * @param selectedContentType the content type selected through content negotiation 
  33.     * @param selectedConverterType the converter type selected to write to the response 
  34.     * @param request the current request 
  35.     * @param response the current response 
  36.     * @return the body that was passed in or a modified, possibly new instance 
  37.     */  
  38.    T beforeBodyWrite(T body, MethodParameter returnType, MediaType selectedContentType,  
  39.          Class<? extends HttpMessageConverter<?>> selectedConverterType,  
  40.          ServerHttpRequest request, ServerHttpResponse response);  
  41.   
  42. }  
package org.springframework.web.servlet.mvc.method.annotation;/** * Allows customizing the response after the execution of an {
    @code @ResponseBody} * or an {
    @code ResponseEntity} controller method but before the body is written * with an {
    @code HttpMessageConverter}. * * <p>Implementations may be may be registered directly with * {
    @code RequestMappingHandlerAdapter} and {
    @code ExceptionHandlerExceptionResolver} * or more likely annotated with {
    @code @ControllerAdvice} in which case they * will be auto-detected by both. * * @author Rossen Stoyanchev * @since 4.1 */public interface ResponseBodyAdvice<T> {   /**    * Whether this component supports the given controller method return type    * and the selected {
    @code HttpMessageConverter} type.    * @param returnType the return type    * @param converterType the selected converter type    * @return {
    @code true} if {
    @link #beforeBodyWrite} should be invoked, {
    @code false} otherwise    */   boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType);   /**    * Invoked after an {
    @code HttpMessageConverter} is selected and just before    * its write method is invoked.    * @param body the body to be written    * @param returnType the return type of the controller method    * @param selectedContentType the content type selected through content negotiation    * @param selectedConverterType the converter type selected to write to the response    * @param request the current request    * @param response the current response    * @return the body that was passed in or a modified, possibly new instance    */   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值