Code Tools
FastJSON 设置不重复利用对象$.ref{}
JSON.toJSONString(data, SerializerFeature.DisableCircularReferenceDetect);
JSON.toJSONStringWithDateFormat(data, DEFAULT_DATE_TIME_FORMAT, SerializerFeature.DisableCircularReferenceDetect);
使用Spring RequestContextHolder 获取请求 request 与 response
public static HttpServletRequest getHttpServletRequest() {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
}
public static HttpServletResponse getHttpServletResponse() {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
}
利用spring来读取配置文件 - [简约]
1.日常读取文件
String kpath = new Object() {
}.getClass().getEnclosingClass().getResource("/").toString()
.replace("/WEB-INF/classes/", "").replace("file:", "");
log.info(" path {}", kpath);
File file = new File(kpath);
kpath = file.getParentFile().getPath();
kpath += File.separator + "classes" + File.separator + "config" + File.separator + config_file;
file = new File(kpath);
if (!file.exists()) {
return null;
}
FileInputStream inputStream = new FileInputStream(file);
int length = inputStream.available();
byte bytes[] = new byte[length];
inputStream.read(bytes);
inputStream.close();
String str = new String(bytes, StandardCharsets.UTF_8);
return str;
2.Spring方式的读取
Resource resource = new ClassPathResource("/config/buildModel.txt");
InputStream inputStream = resource.getInputStream();
String text = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8"));
log.info("content : {}", text);
return text;