All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.weibo.rill.flow.service.interceptor.MapResponseBodyAdvice Maven / Gradle / Ivy

There is a newer version: 0.1.18
Show newest version
/*
 *  Copyright 2021-2023 Weibo, Inc.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.weibo.rill.flow.service.interceptor;

import org.slf4j.MDC;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.HashMap;
import java.util.Map;

@Order(2)
@ControllerAdvice("com.weibo.rill.flow")
public class MapResponseBodyAdvice implements ResponseBodyAdvice {
    public static final String REQUEST_ID_KEY = "request_id";

    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return returnType.getMethod().getReturnType().isAssignableFrom(Map.class);
    }

    @Override
    public Map beforeBodyWrite(Map body, MethodParameter returnType, MediaType selectedContentType, Class> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        Map responseMap = new HashMap<>();
        responseMap.put(REQUEST_ID_KEY, MDC.get(REQUEST_ID_KEY));
        if (body != null) {
            responseMap.putAll((Map) body);
        }
        return responseMap;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy