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

cn.hiboot.mcn.autoconfigure.web.mvc.ResponseUtils Maven / Gradle / Ivy

package cn.hiboot.mcn.autoconfigure.web.mvc;

import cn.hiboot.mcn.core.model.result.RestResp;
import cn.hiboot.mcn.core.util.JacksonUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/**
 * ResponseUtils
 *
 * @author DingHao
 * @since 2023/1/20 23:27
 */
public class ResponseUtils {

    public static  void success(T data, HttpServletResponse response){
        write(new RestResp<>(data),response);
    }

    public static  void success(T data,Long count, HttpServletResponse response){
        write(new RestResp<>(data,count),response);
    }

    public static void failed(String msg, HttpServletResponse response){
        write(RestResp.error(msg),response);
    }

    public static void failed(Integer code,HttpServletResponse response){
        write(RestResp.error(code),response);
    }

    public static void failed(Integer code,String msg, HttpServletResponse response){
        write(RestResp.error(code,msg),response);
    }

    public static  void write(RestResp resp, HttpServletResponse response){
        write(JacksonUtils.toJson(resp),response);
    }

    public static void write(String str, HttpServletResponse response) {
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setStatus(HttpServletResponse.SC_OK);
        try(PrintWriter out = response.getWriter()){
            out.write(str);
            out.flush();
        } catch (IOException e) {
            //ignore
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy