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

com.gitee.easyopen.ApiRespWriter Maven / Gradle / Ivy

Go to download

easyopen mini版,保留基本签名校验,文档功能。https://gitee.com/durcframework/easyopen

There is a newer version: 1.0.4
Show newest version
package com.gitee.easyopen;

import com.gitee.easyopen.bean.Consts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 负责结果输出
 *
 * @author tanghc
 */
public class ApiRespWriter implements RespWriter {

    private static Logger logger = LoggerFactory.getLogger(ApiRespWriter.class);

    @Override
    public void write(HttpServletRequest request, HttpServletResponse response, Object result) {
        if (result == null) {
            return;
        }
        ApiConfig apiConfig = ApiContext.getApiConfig();
        String returnText = apiConfig.getJsonResultSerializer().serialize(result);
        String contentType = MediaType.APPLICATION_JSON_UTF8_VALUE;
        doWriter(response, contentType, returnText);
    }

    /**
     * 发送内容
     *
     * @param response
     * @param contentType
     * @param text
     */
    public static void doWriter(HttpServletResponse response, String contentType, String text) {
        response.setContentType(contentType);
        response.setCharacterEncoding(Consts.UTF8);
        try {
            response.getWriter().write(text);
        } catch (IOException e) {
            logger.error("doWriter", e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy