org.ssssssss.magicapi.modules.ResponseModule Maven / Gradle / Ivy
package org.ssssssss.magicapi.modules;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.ssssssss.magicapi.context.RequestContext;
import org.ssssssss.magicapi.provider.ResultProvider;
import org.ssssssss.script.annotation.Comment;
import org.ssssssss.script.functions.ObjectConvertExtension;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
/**
* response模块
*/
public class ResponseModule {
private final ResultProvider resultProvider;
public ResponseModule(ResultProvider resultProvider) {
this.resultProvider = resultProvider;
}
/**
* 文件下载
*
* @param value 文件内容
* @param filename 文件名
*/
@Comment("文件下载")
public static ResponseEntity> download(@Comment("文件内容,如`byte[]`") Object value, @Comment("文件名") String filename) throws UnsupportedEncodingException {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"))
.body(value);
}
/**
* 自行构建分页结果
*
* @param total 条数
* @param values 数据内容
*/
@Comment("返回自定义分页结果")
public Object page(@Comment("总条数") long total, @Comment("当前结果集") List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy