Skip to content

Commit

Permalink
ResponseUtil writeText writeJson 当characterEncoding 参数是null或者empty的时候,
Browse files Browse the repository at this point in the history
使用默认的utf-8 fix #352

ResponseUtil.writeJson 注释把 默认utf-8 加上 fix #351
  • Loading branch information
venusdrogon committed Jun 9, 2022
1 parent 2ae9367 commit 82135f2
Showing 1 changed file with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ public static void setCacheHeader(HttpServletResponse response,int cacheTime){

/**
* 以text的方式输出.
*
*
* <p>
* 默认 utf-8编码格式输出,如果你需要修改编码格式,可以使用 {@link #writeText(HttpServletResponse, Object, String)}方法
* </p>
*
* @param response
* HttpServletResponse
* @param text
Expand All @@ -313,30 +317,35 @@ public static void writeText(HttpServletResponse response,Object text){

/**
* 以text的方式输出.
*
*
* <p>
* 如果 <code>characterEncoding</code> 是null或者blank,默认使用utf-8
* </p>
*
* @param response
* HttpServletResponse
* @param text
* json字符串
* @param characterEncoding
* 编码<br>
* 如果 <code>characterEncoding</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>characterEncoding</code> 是blank,抛出 {@link IllegalArgumentException}<br>
* 如果 <code>characterEncoding</code> 是null或者blank,默认使用utf-8
* @see #write(HttpServletResponse, Object, String, String)
* @see com.feilong.io.entity.MimeType#TXT
* @since 1.10.6
*/
public static void writeText(HttpServletResponse response,Object text,String characterEncoding){
Validate.notBlank(characterEncoding, "characterEncoding can't be blank!");

String contentType = MimeType.TXT.getMime() + ";charset=" + characterEncoding;
String contentType = MimeType.TXT.getMime() + ";charset=" + defaultIfNullOrEmpty(characterEncoding, UTF8);
write(response, text, contentType, characterEncoding);
}

//---------------------------------------------------------------

/**
* 以json的方式输出.
*
* <p>
* 默认 utf-8编码格式输出,如果你需要修改编码格式,可以使用 {@link #writeJson(HttpServletResponse, Object, String)}方法
* </p>
*
* @param response
* HttpServletResponse
Expand All @@ -352,31 +361,36 @@ public static void writeJson(HttpServletResponse response,Object json){

/**
* 以json的方式输出.
*
* <p>
* 如果 <code>characterEncoding</code> 是null或者blank,默认使用utf-8
* </p>
*
* @param response
* HttpServletResponse
* @param json
* json字符串
* @param characterEncoding
* 编码<br>
* 如果 <code>characterEncoding</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>characterEncoding</code> 是blank,抛出 {@link IllegalArgumentException}<br>
* 如果 <code>characterEncoding</code> 是null或者blank,默认使用utf-8
* @see #write(HttpServletResponse, Object, String, String)
* @see com.feilong.io.entity.MimeType#JSON
* @since 1.0.9
*/
public static void writeJson(HttpServletResponse response,Object json,String characterEncoding){
Validate.notBlank(characterEncoding, "characterEncoding can't be blank!");

String contentType = MimeType.JSON.getMime() + ";charset=" + characterEncoding;
String contentType = MimeType.JSON.getMime() + ";charset=" + defaultIfNullOrEmpty(characterEncoding, UTF8);
write(response, json, contentType, characterEncoding);
}

//---------------------------------------------------------------

/**
* 输出.
*
*
* <p>
* 默认 utf-8编码格式输出,如果你需要修改编码格式,可以使用 {@link #write(HttpServletResponse, Object, String, String)}方法
* </p>
*
* @param response
* HttpServletResponse
* @param content
Expand Down Expand Up @@ -404,8 +418,8 @@ public static void write(HttpServletResponse response,Object content){
* 如果不为null或者empty,将会设置 {@link ServletResponse#setContentType(String)}
* @param characterEncoding
* 字符编码,建议使用 {@link CharsetType} 定义好的常量;<br>
* 可以为null或者empty; <br>
* 如果不为null或者empty,将会设置 {@link ServletResponse#setCharacterEncoding(String)}
* 可以为null或者empty,默认使用utf-8; <br>
* 被用于设置 {@link ServletResponse#setCharacterEncoding(String)}
* @see javax.servlet.ServletResponse#getWriter()
* @see java.io.PrintWriter#print(Object)
* @see java.io.PrintWriter#flush()
Expand Down

0 comments on commit 82135f2

Please sign in to comment.