Spring Webflux 源碼閱讀之 result包

Package org.springframework.web.reactive.result

支持各種編程模型樣式嘹悼,包括調(diào)用不同類型的handles

handlerResultSupport.png

HandlerResultHandlerSupport

HandlerResultHandler的基類囤屹,支持內(nèi)容協(xié)商和訪問ReactiveAdapter注冊表拐袜。

    public abstract class HandlerResultHandlerSupport implements Ordered {

private static final MediaType MEDIA_TYPE_APPLICATION_ALL = new MediaType("application");


private final RequestedContentTypeResolver contentTypeResolver;

private final ReactiveAdapterRegistry adapterRegistry;

private int order = LOWEST_PRECEDENCE;


protected HandlerResultHandlerSupport(RequestedContentTypeResolver contentTypeResolver,
        ReactiveAdapterRegistry adapterRegistry) {

    Assert.notNull(contentTypeResolver, "RequestedContentTypeResolver is required");
    Assert.notNull(adapterRegistry, "ReactiveAdapterRegistry is required");
    this.contentTypeResolver = contentTypeResolver;
    this.adapterRegistry = adapterRegistry;
}


/**
 * Return the configured {@link ReactiveAdapterRegistry}.
 */
public ReactiveAdapterRegistry getAdapterRegistry() {
    return this.adapterRegistry;
}

/**
 * Return the configured {@link RequestedContentTypeResolver}.
 */
public RequestedContentTypeResolver getContentTypeResolver() {
    return this.contentTypeResolver;
}

/**
 * Set the order for this result handler relative to others.
 * <p>By default set to {@link Ordered#LOWEST_PRECEDENCE}, however see
 * Javadoc of sub-classes which may change this default.
 * @param order the order
 */
public void setOrder(int order) {
    this.order = order;
}

@Override
public int getOrder() {
    return this.order;
}


/**
 * Get a {@code ReactiveAdapter} for the top-level return value type.
 * @return the matching adapter or {@code null}
 */
@Nullable
protected ReactiveAdapter getAdapter(HandlerResult result) {
    Class<?> returnType = result.getReturnType().getRawClass();
    return getAdapterRegistry().getAdapter(returnType, result.getReturnValue());
}

/**
 * Select the best media type for the current request through a content
 * negotiation algorithm.
 * @param exchange the current request
 * @param producibleTypesSupplier the media types that can be produced for the current request
 * @return the selected media type or {@code null}
 */
@Nullable
protected MediaType selectMediaType(ServerWebExchange exchange,
        Supplier<List<MediaType>> producibleTypesSupplier) {

    List<MediaType> acceptableTypes = getAcceptableTypes(exchange);
    List<MediaType> producibleTypes = getProducibleTypes(exchange, producibleTypesSupplier);

    Set<MediaType> compatibleMediaTypes = new LinkedHashSet<>();
    for (MediaType acceptable : acceptableTypes) {
        for (MediaType producible : producibleTypes) {
            if (acceptable.isCompatibleWith(producible)) {
                compatibleMediaTypes.add(selectMoreSpecificMediaType(acceptable, producible));
            }
        }
    }

    List<MediaType> result = new ArrayList<>(compatibleMediaTypes);
    MediaType.sortBySpecificityAndQuality(result);

    for (MediaType mediaType : result) {
        if (mediaType.isConcrete()) {
            return mediaType;
        }
        else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICATION_ALL)) {
            return MediaType.APPLICATION_OCTET_STREAM;
        }
    }

    return null;
}

private List<MediaType> getAcceptableTypes(ServerWebExchange exchange) {
    List<MediaType> mediaTypes = getContentTypeResolver().resolveMediaTypes(exchange);
    return (mediaTypes.isEmpty() ? Collections.singletonList(MediaType.ALL) : mediaTypes);
}

@SuppressWarnings("unchecked")
private List<MediaType> getProducibleTypes(ServerWebExchange exchange,
        Supplier<List<MediaType>> producibleTypesSupplier) {

    Set<MediaType> mediaTypes = exchange.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
    return (mediaTypes != null ? new ArrayList<>(mediaTypes) : producibleTypesSupplier.get());
}

private MediaType selectMoreSpecificMediaType(MediaType acceptable, MediaType producible) {
    producible = producible.copyQualityValue(acceptable);
    Comparator<MediaType> comparator = MediaType.SPECIFICITY_COMPARATOR;
    return (comparator.compare(acceptable, producible) <= 0 ? acceptable : producible);
}

}

接口 View的Diagram

view.png

View

通過視圖解析支持結(jié)果處理派哲。

將HandlerResult呈現(xiàn)給HTTP響應(yīng)的約定。

與Encoder相比答朋,Encoder是一個單實(shí)例對象津辩,并對給定類型的任何對象進(jìn)行編碼,因此施无,視圖通常是通過名稱來選擇的辉词,并使用ViewResolver來解析,例如將其與HTML模板匹配猾骡。此外瑞躺,視圖可以基于模型中包含的多個屬性呈現(xiàn)敷搪。

視圖還可以選擇從模型中選擇一個屬性,使用任何現(xiàn)有的編碼器來呈現(xiàn)替代媒體類型幢哨。

返回此視圖支持的媒體類型列表赡勘,或空列表。
List<MediaType> getSupportedMediaTypes();

這個視圖是否通過執(zhí)行重定向來呈現(xiàn)捞镰。
default boolean isRedirectView() {
    return false;
}

根據(jù)給定的HandlerResult呈現(xiàn)視圖闸与。實(shí)現(xiàn)可以訪問和使用模型,或者僅在其中使用一個特定的屬性岸售。
Mono<Void> render(@Nullable Map<String, ?> model, @Nullable MediaType contentType, ServerWebExchange exchange);

AbstractView

View實(shí)現(xiàn)的基類践樱。


public abstract class AbstractView implements View, ApplicationContextAware {

    /**  在 bean factory 有名的RequestDataValueProcessor */
    public static final String REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME = "requestDataValueProcessor";


    /** 可用于子類的日志記錄器 */
    protected final Log logger = LogFactory.getLog(getClass());

    private static final Object NO_VALUE = new Object();


    private final List<MediaType> mediaTypes = new ArrayList<>(4);

    private final ReactiveAdapterRegistry adapterRegistry;

    private Charset defaultCharset = StandardCharsets.UTF_8;

    @Nullable
    private String requestContextAttribute;

    @Nullable
    private ApplicationContext applicationContext;


    public AbstractView() {
        this(new ReactiveAdapterRegistry());
    }

    public AbstractView(ReactiveAdapterRegistry registry) {
        this.mediaTypes.add(ViewResolverSupport.DEFAULT_CONTENT_TYPE);
        this.adapterRegistry = registry;
    }


    /**
     * Set the supported media types for this view.
     * Default is "text/html;charset=UTF-8".
     */
    public void setSupportedMediaTypes(@Nullable List<MediaType> supportedMediaTypes) {
        Assert.notEmpty(supportedMediaTypes, "MediaType List must not be empty");
        this.mediaTypes.clear();
        if (supportedMediaTypes != null) {
            this.mediaTypes.addAll(supportedMediaTypes);
        }
    }

    /**
     * Return the configured media types supported by this view.
     */
    @Override
    public List<MediaType> getSupportedMediaTypes() {
        return this.mediaTypes;
    }

    /**
     * Set the default charset for this view, used when the
     * {@linkplain #setSupportedMediaTypes(List) content type} does not contain one.
     * Default is {@linkplain StandardCharsets#UTF_8 UTF 8}.
     */
    public void setDefaultCharset(Charset defaultCharset) {
        Assert.notNull(defaultCharset, "'defaultCharset' must not be null");
        this.defaultCharset = defaultCharset;
    }

    /**
     * Return the default charset, used when the
     * {@linkplain #setSupportedMediaTypes(List) content type} does not contain one.
     */
    public Charset getDefaultCharset() {
        return this.defaultCharset;
    }

    /**
     * Set the name of the RequestContext attribute for this view.
     * Default is none.
     */
    public void setRequestContextAttribute(@Nullable String requestContextAttribute) {
        this.requestContextAttribute = requestContextAttribute;
    }

    /**
     * Return the name of the RequestContext attribute, if any.
     */
    @Nullable
    public String getRequestContextAttribute() {
        return this.requestContextAttribute;
    }

    @Override
    public void setApplicationContext(@Nullable ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Nullable
    public ApplicationContext getApplicationContext() {
        return this.applicationContext;
    }

    /**
     * Obtain the ApplicationContext for actual use.
     * @return the ApplicationContext (never {@code null})
     * @throws IllegalStateException in case of no ApplicationContext set
     */
    protected final ApplicationContext obtainApplicationContext() {
        ApplicationContext applicationContext = getApplicationContext();
        Assert.state(applicationContext != null, "No ApplicationContext");
        return applicationContext;
    }


    /**
     * Prepare the model to render.
     * @param model Map with name Strings as keys and corresponding model
     * objects as values (Map can also be {@code null} in case of empty model)
     * @param contentType the content type selected to render with which should
     * match one of the {@link #getSupportedMediaTypes() supported media types}.
     * @param exchange the current exchange
     * @return {@code Mono} to represent when and if rendering succeeds
     */
    @Override
    public Mono<Void> render(@Nullable Map<String, ?> model, @Nullable MediaType contentType,
            ServerWebExchange exchange) {

        if (logger.isTraceEnabled()) {
            logger.trace("Rendering view with model " + model);
        }

        if (contentType != null) {
            exchange.getResponse().getHeaders().setContentType(contentType);
        }

        return getModelAttributes(model, exchange).flatMap(mergedModel -> {
            // Expose RequestContext?
            if (this.requestContextAttribute != null) {
                mergedModel.put(this.requestContextAttribute, createRequestContext(exchange, mergedModel));
            }
            return renderInternal(mergedModel, contentType, exchange);
        });
    }

    /**
     * Prepare the model to use for rendering.
     * <p>The default implementation creates a combined output Map that includes
     * model as well as static attributes with the former taking precedence.
     */
    protected Mono<Map<String, Object>> getModelAttributes(@Nullable Map<String, ?> model,
            ServerWebExchange exchange) {

        int size = (model != null ? model.size() : 0);

        Map<String, Object> attributes = new LinkedHashMap<>(size);
        if (model != null) {
            attributes.putAll(model);
        }

        return resolveAsyncAttributes(attributes).then(Mono.just(attributes));
    }

    /**
     * By default, resolve async attributes supported by the
     * {@link ReactiveAdapterRegistry} to their blocking counterparts.
     * <p>View implementations capable of taking advantage of reactive types
     * can override this method if needed.
     * @return {@code Mono} for the completion of async attributes resolution
     */
    protected Mono<Void> resolveAsyncAttributes(Map<String, Object> model) {

        List<String> names = new ArrayList<>();
        List<Mono<?>> valueMonos = new ArrayList<>();

        for (Map.Entry<String, ?> entry : model.entrySet()) {
            Object value =  entry.getValue();
            if (value == null) {
                continue;
            }
            ReactiveAdapter adapter = this.adapterRegistry.getAdapter(null, value);
            if (adapter != null) {
                names.add(entry.getKey());
                if (adapter.isMultiValue()) {
                    Flux<Object> fluxValue = Flux.from(adapter.toPublisher(value));
                    valueMonos.add(fluxValue.collectList().defaultIfEmpty(Collections.emptyList()));
                }
                else {
                    Mono<Object> monoValue = Mono.from(adapter.toPublisher(value));
                    valueMonos.add(monoValue.defaultIfEmpty(NO_VALUE));
                }
            }
        }

        if (names.isEmpty()) {
            return Mono.empty();
        }

        return Mono.zip(valueMonos,
                values -> {
                    for (int i=0; i < values.length; i++) {
                        if (values[i] != NO_VALUE) {
                            model.put(names.get(i), values[i]);
                        }
                        else {
                            model.remove(names.get(i));
                        }
                    }
                    return NO_VALUE;
                })
                .then();
    }

    /**
     * Create a RequestContext to expose under the specified attribute name.
     * <p>The default implementation creates a standard RequestContext instance
     * for the given request and model. Can be overridden in subclasses for
     * custom instances.
     * @param exchange current exchange
     * @param model combined output Map (never {@code null}),
     * with dynamic values taking precedence over static attributes
     * @return the RequestContext instance
     * @see #setRequestContextAttribute
     */
    protected RequestContext createRequestContext(ServerWebExchange exchange, Map<String, Object> model) {
        return new RequestContext(exchange, model, obtainApplicationContext(), getRequestDataValueProcessor());
    }

    /**
     * Return the {@link RequestDataValueProcessor} to use.
     * <p>The default implementation looks in the {@link #getApplicationContext()
     * Spring configuration} for a {@code RequestDataValueProcessor} bean with
     * the name {@link #REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME}.
     * @return the RequestDataValueProcessor, or null if there is none at the
     * application context.
     */
    @Nullable
    protected RequestDataValueProcessor getRequestDataValueProcessor() {
        ApplicationContext context = getApplicationContext();
        if (context != null && context.containsBean(REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
            return context.getBean(REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
        }
        return null;
    }

    /**
     * Subclasses must implement this method to actually render the view.
     * @param renderAttributes combined output Map (never {@code null}),
     * with dynamic values taking precedence over static attributes
     * @param contentType the content type selected to render with which should
     * match one of the {@link #getSupportedMediaTypes() supported media types}.
     *@param exchange current exchange  @return {@code Mono} to represent when
     * and if rendering succeeds
     */
    protected abstract Mono<Void> renderInternal(Map<String, Object> renderAttributes,
            @Nullable MediaType contentType, ServerWebExchange exchange);


    @Override
    public String toString() {
        return getClass().getName();
    }

}

ViewResolver

ViewResolver.png

Rendering

reanding.jpg

SimpleHandlerAdapter

HandlerAdapter,它允許使用普通的WebHandler與一般的DispatcherHandler一起使用凸丸。



public class SimpleHandlerAdapter implements HandlerAdapter {

    private static final MethodParameter RETURN_TYPE;

    static {
        try {
            Method method = WebHandler.class.getMethod("handle", ServerWebExchange.class);
            RETURN_TYPE = new MethodParameter(method, -1);
        }
        catch (NoSuchMethodException ex) {
            throw new IllegalStateException(
                    "Failed to initialize the return type for WebHandler: " + ex.getMessage());
        }
    }

        //這個HandlerAdapter是否支持給定的handler拷邢。
    @Override
    public boolean supports(Object handler) {
        return WebHandler.class.isAssignableFrom(handler.getClass());
    }

      //鼓勵實(shí)現(xiàn)處理由調(diào)用handler所產(chǎn)生的異常,并在必要時返回代表錯誤響應(yīng)的替代結(jié)果屎慢。
    @Override
    public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) {
        WebHandler webHandler = (WebHandler) handler;
        Mono<Void> mono = webHandler.handle(exchange);
        return mono.then(Mono.empty());
    }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末瞭稼,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子腻惠,更是在濱河造成了極大的恐慌环肘,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件集灌,死亡現(xiàn)場離奇詭異廷臼,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)绝页,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進(jìn)店門荠商,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人续誉,你說我怎么就攤上這事莱没。” “怎么了酷鸦?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵饰躲,是天一觀的道長。 經(jīng)常有香客問我臼隔,道長嘹裂,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任摔握,我火速辦了婚禮寄狼,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己泊愧,他們只是感情好伊磺,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著删咱,像睡著了一般屑埋。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上痰滋,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天摘能,我揣著相機(jī)與錄音,去河邊找鬼敲街。 笑死徊哑,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的聪富。 我是一名探鬼主播,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼著蟹,長吁一口氣:“原來是場噩夢啊……” “哼墩蔓!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起萧豆,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤奸披,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后涮雷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體阵面,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年洪鸭,在試婚紗的時候發(fā)現(xiàn)自己被綠了样刷。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡览爵,死狀恐怖置鼻,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蜓竹,我是刑警寧澤箕母,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站俱济,受9級特大地震影響嘶是,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蛛碌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一聂喇、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蔚携,春花似錦授帕、人聲如沸同木。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽彤路。三九已至,卻和暖如春芥映,著一層夾襖步出監(jiān)牢的瞬間洲尊,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工奈偏, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留坞嘀,地道東北人。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓惊来,卻偏偏與公主長得像丽涩,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子裁蚁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容