1. 程式人生 > 其它 >解決字串替換報錯的問題

解決字串替換報錯的問題

技術標籤:日常擼碼

問題:

The following has evaluated to null or missing:\n==> url  [in template \"template\" at line 1, column 3]\n\n----\nTip: If the failing expression is known to legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??\n----\n\n----\nFTL stack trace (\"~\" means nesting-related):\n\t- Failed at: ${url}  [in template \"template\" at line 1, column 1]\n----"

原因:這個地方之前約定過messageData是不可能為空,並且是個有值的json串

解決方法:加個map的空值校驗

替換字串方法:

public class ReplaceStrUtils {
	
	// freeMark替換模板內容
	public static String replaceTemplate(String content, Map<String, String> map) throws Exception {
		StringWriter result = new StringWriter();
		Template t = new Template("template", new StringReader(content), new Configuration(Configuration.VERSION_2_3_23));
		t.process(map, result);
		return result.toString();
	}
}