public final class MvcUtil extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
HEADER_X_REQUESTED_WITH_NAME |
static String |
X_REQUESTED_WITH |
| 限定符和类型 | 方法和说明 |
|---|---|
static boolean |
isAjaxOrJson(javax.servlet.http.HttpServletRequest request)
判断是否为 ajax 请求或者支持接收 json 格式
|
static <T> T |
json2Object(String jsonString,
Class<T> clz)
使用
ObjectMapper 把 jsonString 反序列化为 T 对象. |
static byte[] |
readAllBytes(InputStream inputStream)
Copy from
InputStream of JDK11Reads all remaining bytes from the input stream. |
static void |
responseWithJson(javax.servlet.http.HttpServletResponse response,
int status,
String result)
向客户端响应 json 格式
|
static String[] |
splitByCharacterTypeCamelCase(String str,
boolean camelCase)
Copy from
org.apache.commons.lang3.StringUtils
Splits a String by Character type as returned by
java.lang.Character.getType(char). |
static String |
toJsonString(Object obj)
通过
ObjectMapper 转换对象到 JSON 字符串, 主要目的用于日志输出对象字符串时使用, 减少 try catch 嵌套, 转换失败记录日志并返回空字符串. |
public static String toJsonString(Object obj)
ObjectMapper 转换对象到 JSON 字符串, 主要目的用于日志输出对象字符串时使用, 减少 try catch 嵌套, 转换失败记录日志并返回空字符串.obj - Object@Nullable
public static <T> T json2Object(@NonNull
String jsonString,
@NonNull
Class<T> clz)
ObjectMapper 把 jsonString 反序列化为 T 对象.jsonString - json stringclz - 要反序列化的目标 classpublic static boolean isAjaxOrJson(javax.servlet.http.HttpServletRequest request)
request - requestpublic static void responseWithJson(javax.servlet.http.HttpServletResponse response,
int status,
String result)
throws IOException
response - responsestatus - 响应的状态码result - 相应的结果字符串IOException - IOExceptionpublic static String[] splitByCharacterTypeCamelCase(String str, boolean camelCase)
org.apache.commons.lang3.StringUtils
Splits a String by Character type as returned by
java.lang.Character.getType(char). Groups of contiguous
characters of the same type are returned as complete tokens, with the
following exception: the character of type
Character.UPPERCASE_LETTER, if any, immediately
preceding a token of type Character.LOWERCASE_LETTER
will belong to the following token rather than to the preceding, if any,
Character.UPPERCASE_LETTER token.
StringUtils.splitByCharacterTypeCamelCase(null) = null
StringUtils.splitByCharacterTypeCamelCase("") = []
StringUtils.splitByCharacterTypeCamelCase("ab de fg") = ["ab", " ", "de", " ", "fg"]
StringUtils.splitByCharacterTypeCamelCase("ab de fg") = ["ab", " ", "de", " ", "fg"]
StringUtils.splitByCharacterTypeCamelCase("ab:cd:ef") = ["ab", ":", "cd", ":", "ef"]
StringUtils.splitByCharacterTypeCamelCase("number5") = ["number", "5"]
StringUtils.splitByCharacterTypeCamelCase("fooBar") = ["foo", "Bar"]
StringUtils.splitByCharacterTypeCamelCase("foo200Bar") = ["foo", "200", "Bar"]
StringUtils.splitByCharacterTypeCamelCase("ASFRules") = ["ASF", "Rules"]
str - the String to split, may be nullcamelCase - whether to use so-called "camel-case" for letter typesnull if null String inputpublic static byte[] readAllBytes(InputStream inputStream) throws IOException
InputStream of JDK11When this stream reaches end of stream, further invocations of this method will return an empty byte array.
Note that this method is intended for simple cases where it is convenient to read all bytes into a byte array. It is not intended for reading input streams with large amounts of data.
The behavior for the case where the input stream is asynchronously closed, or the thread interrupted during the read, is highly input stream specific, and therefore not specified.
If an I/O error occurs reading from the input stream, then it may do so after some, but not all, bytes have been read. Consequently the input stream may not be at end of stream and may be in an inconsistent state. It is strongly recommended that the stream be promptly closed if an I/O error occurs.
inputStream - InputStreamIOException - if an I/O error occursOutOfMemoryError - if an array of the required size cannot be
allocated.Copyright © 2021. All rights reserved.