Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
guns-vip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chenjunxiong
guns-vip
Commits
a1c700f4
Commit
a1c700f4
authored
Mar 30, 2017
by
fsn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完成异常落库
parent
980d11c8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
436 additions
and
438 deletions
+436
-438
src/main/java/com/stylefeng/guns/core/aop/GlobalExceptionHandler.java
+11
-0
src/main/java/com/stylefeng/guns/core/log/LogFactory.java
+5
-1
src/main/java/com/stylefeng/guns/core/util/ToolUtil.java
+420
-437
No files found.
src/main/java/com/stylefeng/guns/core/aop/GlobalExceptionHandler.java
View file @
a1c700f4
...
@@ -3,11 +3,15 @@ package com.stylefeng.guns.core.aop;
...
@@ -3,11 +3,15 @@ package com.stylefeng.guns.core.aop;
import
com.stylefeng.guns.common.constant.tips.ErrorTip
;
import
com.stylefeng.guns.common.constant.tips.ErrorTip
;
import
com.stylefeng.guns.common.exception.BizExceptionEnum
;
import
com.stylefeng.guns.common.exception.BizExceptionEnum
;
import
com.stylefeng.guns.common.exception.BussinessException
;
import
com.stylefeng.guns.common.exception.BussinessException
;
import
com.stylefeng.guns.core.log.ILog
;
import
com.stylefeng.guns.core.support.HttpKit
;
import
com.stylefeng.guns.core.support.HttpKit
;
import
com.stylefeng.guns.core.util.SpringContextHolder
;
import
com.stylefeng.guns.core.util.ToolUtil
;
import
org.apache.log4j.Logger
;
import
org.apache.log4j.Logger
;
import
org.apache.shiro.authc.AuthenticationException
;
import
org.apache.shiro.authc.AuthenticationException
;
import
org.apache.shiro.authc.CredentialsException
;
import
org.apache.shiro.authc.CredentialsException
;
import
org.apache.shiro.authc.DisabledAccountException
;
import
org.apache.shiro.authc.DisabledAccountException
;
import
org.springframework.context.annotation.DependsOn
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.ui.Model
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
...
@@ -24,10 +28,14 @@ import javax.naming.NoPermissionException;
...
@@ -24,10 +28,14 @@ import javax.naming.NoPermissionException;
* @date 2016年11月12日 下午3:19:56
* @date 2016年11月12日 下午3:19:56
*/
*/
@ControllerAdvice
@ControllerAdvice
@DependsOn
(
"springContextHolder"
)
public
class
GlobalExceptionHandler
{
public
class
GlobalExceptionHandler
{
private
Logger
log
=
Logger
.
getLogger
(
this
.
getClass
());
private
Logger
log
=
Logger
.
getLogger
(
this
.
getClass
());
private
ILog
logFactory
=
SpringContextHolder
.
getBean
(
ILog
.
class
);
/**
/**
* 拦截业务异常
* 拦截业务异常
*
*
...
@@ -37,6 +45,7 @@ public class GlobalExceptionHandler {
...
@@ -37,6 +45,7 @@ public class GlobalExceptionHandler {
@ResponseStatus
(
HttpStatus
.
INTERNAL_SERVER_ERROR
)
@ResponseStatus
(
HttpStatus
.
INTERNAL_SERVER_ERROR
)
@ResponseBody
@ResponseBody
public
ErrorTip
notFount
(
BussinessException
e
)
{
public
ErrorTip
notFount
(
BussinessException
e
)
{
logFactory
.
doLog
(
"业务异常"
,
e
.
toString
(),
false
);
HttpKit
.
getRequest
().
setAttribute
(
"tip"
,
e
.
getMessage
());
HttpKit
.
getRequest
().
setAttribute
(
"tip"
,
e
.
getMessage
());
return
new
ErrorTip
(
e
.
getCode
(),
e
.
getMessage
());
return
new
ErrorTip
(
e
.
getCode
(),
e
.
getMessage
());
}
}
...
@@ -50,6 +59,8 @@ public class GlobalExceptionHandler {
...
@@ -50,6 +59,8 @@ public class GlobalExceptionHandler {
@ResponseStatus
(
HttpStatus
.
INTERNAL_SERVER_ERROR
)
@ResponseStatus
(
HttpStatus
.
INTERNAL_SERVER_ERROR
)
@ResponseBody
@ResponseBody
public
ErrorTip
notFount
(
RuntimeException
e
)
{
public
ErrorTip
notFount
(
RuntimeException
e
)
{
String
msg
=
ToolUtil
.
getExceptionMsg
(
e
);
logFactory
.
doLog
(
"Runtime异常"
,
msg
,
false
);
log
.
error
(
"服务器异常:"
,
e
);
log
.
error
(
"服务器异常:"
,
e
);
HttpKit
.
getRequest
().
setAttribute
(
"tip"
,
"服务器未知运行时异常"
);
HttpKit
.
getRequest
().
setAttribute
(
"tip"
,
"服务器未知运行时异常"
);
return
new
ErrorTip
(
BizExceptionEnum
.
SERVER_ERROR
);
return
new
ErrorTip
(
BizExceptionEnum
.
SERVER_ERROR
);
...
...
src/main/java/com/stylefeng/guns/core/log/LogFactory.java
View file @
a1c700f4
...
@@ -30,6 +30,10 @@ public class LogFactory implements ILog {
...
@@ -30,6 +30,10 @@ public class LogFactory implements ILog {
log
.
setSucceed
((
succeed
)
?
"1"
:
"0"
);
log
.
setSucceed
((
succeed
)
?
"1"
:
"0"
);
log
.
setUserid
(
String
.
valueOf
(
user
.
getId
()));
log
.
setUserid
(
String
.
valueOf
(
user
.
getId
()));
log
.
setLogname
(
logName
);
log
.
setLogname
(
logName
);
operationLogMapper
.
insert
(
log
);
try
{
operationLogMapper
.
insert
(
log
);
}
catch
(
Exception
e
){
//e.printStackTrace();
}
}
}
}
}
src/main/java/com/stylefeng/guns/core/util/ToolUtil.java
View file @
a1c700f4
/**
/**
* Copyright (c) 2015-2016, Chill Zhuang 庄骞 (smallchill@163.com).
* Copyright (c) 2015-2016, Chill Zhuang 庄骞 (smallchill@163.com).
*
*
<p>
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* You may obtain a copy of the License at
*
*
<p>
*
http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
*
<p>
* Unless required by applicable law or agreed to in writing, software
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
...
@@ -15,402 +15,384 @@
...
@@ -15,402 +15,384 @@
*/
*/
package
com
.
stylefeng
.
guns
.
core
.
util
;
package
com
.
stylefeng
.
guns
.
core
.
util
;
import
com.stylefeng.guns.core.support.StrKit
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
java.io.StringWriter
;
import
java.lang.reflect.Array
;
import
java.lang.reflect.Array
;
import
java.util.Arrays
;
import
java.util.*
;
import
java.util.Calendar
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.Enumeration
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Random
;
import
java.util.Map.Entry
;
import
java.util.Map.Entry
;
import
java.util.Set
;
import
com.stylefeng.guns.core.support.StrKit
;
/**
/**
* 高频方法集合类
* 高频方法集合类
*/
*/
public
class
ToolUtil
{
public
class
ToolUtil
{
/**
/**
* 获取异常的具体信息
* 获取随机车牌号码
*
* @date 2017年2月24日 下午10:12:52
* @author fengshuonan
*/
* @Date 2017/3/30 9:21
public
static
String
randomPlateNumber
(){
* @version 2.0
// String province = "冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼渝京津沪";
*/
String
province
=
"京"
;
public
static
String
getExceptionMsg
(
Exception
e
)
{
String
letters
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
;
StringWriter
sw
=
new
StringWriter
();
String
nums
=
"0123456789"
;
try
{
Random
random
=
new
Random
();
e
.
printStackTrace
(
new
PrintWriter
(
sw
));
StringBuffer
sb
=
new
StringBuffer
();
}
finally
{
sb
.
append
(
province
.
charAt
(
random
.
nextInt
(
province
.
length
())));
try
{
sb
.
append
(
letters
.
charAt
(
random
.
nextInt
(
letters
.
length
())));
sw
.
close
();
for
(
int
i
=
0
;
i
<
5
;
i
++){
}
catch
(
IOException
e1
)
{
sb
.
append
(
nums
.
charAt
(
random
.
nextInt
(
nums
.
length
())));
e1
.
printStackTrace
();
}
}
return
sb
.
toString
();
}
}
return
sw
.
getBuffer
().
toString
().
replaceAll
(
"\\$"
,
"T"
);
}
/**
* @Description 主键id
/**
* @author fengshuonan
* @Description 主键id
*/
* @author fengshuonan
public
static
String
getUid
(){
*/
return
getRandomNum
();
public
static
String
getUid
()
{
}
return
getRandomNum
();
}
/**
* @Description 随机数字
/**
* @author fengshuonan
* @Description 随机数字
*/
* @author fengshuonan
public
static
String
getRandomNum
(){
*/
return
Calendar
.
getInstance
().
getTimeInMillis
()
+
generateCellPhoneValNum
();
public
static
String
getRandomNum
()
{
}
return
Calendar
.
getInstance
().
getTimeInMillis
()
+
generateCellPhoneValNum
();
}
/**
* @Description 获取电话号码
/**
* @author fengshuonan
* @Description 获取电话号码
*/
* @author fengshuonan
public
static
String
generateCellPhoneValNum
()
{
*/
String
[]
beforeShuffle
=
new
String
[]
{
"1"
,
"2"
,
"3"
,
"4"
,
"5"
,
"6"
,
public
static
String
generateCellPhoneValNum
()
{
"7"
,
"8"
,
"9"
,
"0"
};
String
[]
beforeShuffle
=
new
String
[]{
"1"
,
"2"
,
"3"
,
"4"
,
"5"
,
"6"
,
List
<
String
>
list
=
Arrays
.
asList
(
beforeShuffle
);
"7"
,
"8"
,
"9"
,
"0"
};
Collections
.
shuffle
(
list
);
List
<
String
>
list
=
Arrays
.
asList
(
beforeShuffle
);
StringBuilder
buffer
=
new
StringBuilder
();
Collections
.
shuffle
(
list
);
for
(
int
i
=
0
;
i
<
list
.
size
();
i
++)
{
StringBuilder
buffer
=
new
StringBuilder
();
buffer
.
append
(
list
.
get
(
i
));
for
(
int
i
=
0
;
i
<
list
.
size
();
i
++)
{
}
buffer
.
append
(
list
.
get
(
i
));
String
afterShuffle
=
buffer
.
toString
();
}
String
result
=
afterShuffle
.
substring
(
3
,
9
);
String
afterShuffle
=
buffer
.
toString
();
return
result
;
String
result
=
afterShuffle
.
substring
(
3
,
9
);
}
return
result
;
}
/**
* 比较两个对象是否相等。<br>
/**
* 相同的条件有两个,满足其一即可:<br>
* 比较两个对象是否相等。<br>
* 1. obj1 == null && obj2 == null; 2. obj1.equals(obj2)
* 相同的条件有两个,满足其一即可:<br>
*
* 1. obj1 == null && obj2 == null; 2. obj1.equals(obj2)
* @param obj1
*
* 对象1
* @param obj1 对象1
* @param obj2
* @param obj2 对象2
* 对象2
* @return 是否相等
* @return 是否相等
*/
*/
public
static
boolean
equals
(
Object
obj1
,
Object
obj2
)
{
public
static
boolean
equals
(
Object
obj1
,
Object
obj2
)
{
return
(
obj1
!=
null
)
?
(
obj1
.
equals
(
obj2
))
:
(
obj2
==
null
);
return
(
obj1
!=
null
)
?
(
obj1
.
equals
(
obj2
))
:
(
obj2
==
null
);
}
}
/**
/**
* 计算对象长度,如果是字符串调用其length函数,集合类调用其size函数,数组调用其length属性,其他可遍历对象遍历计算长度
* 计算对象长度,如果是字符串调用其length函数,集合类调用其size函数,数组调用其length属性,其他可遍历对象遍历计算长度
*
*
* @param obj 被计算长度的对象
* @param obj
* @return 长度
* 被计算长度的对象
*/
* @return 长度
public
static
int
length
(
Object
obj
)
{
*/
if
(
obj
==
null
)
{
public
static
int
length
(
Object
obj
)
{
return
0
;
if
(
obj
==
null
)
{
}
return
0
;
if
(
obj
instanceof
CharSequence
)
{
}
return
((
CharSequence
)
obj
).
length
();
if
(
obj
instanceof
CharSequence
)
{
}
return
((
CharSequence
)
obj
).
length
();
if
(
obj
instanceof
Collection
)
{
}
return
((
Collection
<?>)
obj
).
size
();
if
(
obj
instanceof
Collection
)
{
}
return
((
Collection
<?>)
obj
).
size
();
if
(
obj
instanceof
Map
)
{
}
return
((
Map
<?,
?>)
obj
).
size
();
if
(
obj
instanceof
Map
)
{
}
return
((
Map
<?,
?>)
obj
).
size
();
}
int
count
;
if
(
obj
instanceof
Iterator
)
{
int
count
;
Iterator
<?>
iter
=
(
Iterator
<?>)
obj
;
if
(
obj
instanceof
Iterator
)
{
count
=
0
;
Iterator
<?>
iter
=
(
Iterator
<?>)
obj
;
while
(
iter
.
hasNext
())
{
count
=
0
;
count
++;
while
(
iter
.
hasNext
())
{
iter
.
next
();
count
++;
}
iter
.
next
();
return
count
;
}
}
return
count
;
if
(
obj
instanceof
Enumeration
)
{
}
Enumeration
<?>
enumeration
=
(
Enumeration
<?>)
obj
;
if
(
obj
instanceof
Enumeration
)
{
count
=
0
;
Enumeration
<?>
enumeration
=
(
Enumeration
<?>)
obj
;
while
(
enumeration
.
hasMoreElements
())
{
count
=
0
;
count
++;
while
(
enumeration
.
hasMoreElements
())
{
enumeration
.
nextElement
();
count
++;
}
enumeration
.
nextElement
();
return
count
;
}
}
return
count
;
if
(
obj
.
getClass
().
isArray
()
==
true
)
{
}
return
Array
.
getLength
(
obj
);
if
(
obj
.
getClass
().
isArray
()
==
true
)
{
}
return
Array
.
getLength
(
obj
);
return
-
1
;
}
}
return
-
1
;
}
/**
* 对象中是否包含元素
/**
*
* 对象中是否包含元素
* @param obj 对象
*
* @param element 元素
* @param obj
* @return 是否包含
* 对象
*/
* @param element
public
static
boolean
contains
(
Object
obj
,
Object
element
)
{
* 元素
if
(
obj
==
null
)
{
* @return 是否包含
return
false
;
*/
}
public
static
boolean
contains
(
Object
obj
,
Object
element
)
{
if
(
obj
instanceof
String
)
{
if
(
obj
==
null
)
{
if
(
element
==
null
)
{
return
false
;
return
false
;
}
}
if
(
obj
instanceof
String
)
{
return
((
String
)
obj
).
contains
(
element
.
toString
());
if
(
element
==
null
)
{
}
return
false
;
if
(
obj
instanceof
Collection
)
{
}
return
((
Collection
<?>)
obj
).
contains
(
element
);
return
((
String
)
obj
).
contains
(
element
.
toString
());
}
}
if
(
obj
instanceof
Map
)
{
if
(
obj
instanceof
Collection
)
{
return
((
Map
<?,
?>)
obj
).
values
().
contains
(
element
);
return
((
Collection
<?>)
obj
).
contains
(
element
);
}
}
if
(
obj
instanceof
Map
)
{
if
(
obj
instanceof
Iterator
)
{
return
((
Map
<?,
?>)
obj
).
values
().
contains
(
element
);
Iterator
<?>
iter
=
(
Iterator
<?>)
obj
;
}
while
(
iter
.
hasNext
())
{
Object
o
=
iter
.
next
();
if
(
obj
instanceof
Iterator
)
{
if
(
equals
(
o
,
element
))
{
Iterator
<?>
iter
=
(
Iterator
<?>)
obj
;
return
true
;
while
(
iter
.
hasNext
())
{
}
Object
o
=
iter
.
next
();
}
if
(
equals
(
o
,
element
))
{
return
false
;
return
true
;
}
}
if
(
obj
instanceof
Enumeration
)
{
}
Enumeration
<?>
enumeration
=
(
Enumeration
<?>)
obj
;
return
false
;
while
(
enumeration
.
hasMoreElements
())
{
}
Object
o
=
enumeration
.
nextElement
();
if
(
obj
instanceof
Enumeration
)
{
if
(
equals
(
o
,
element
))
{
Enumeration
<?>
enumeration
=
(
Enumeration
<?>)
obj
;
return
true
;
while
(
enumeration
.
hasMoreElements
())
{
}
Object
o
=
enumeration
.
nextElement
();
}
if
(
equals
(
o
,
element
))
{
return
false
;
return
true
;
}
}
if
(
obj
.
getClass
().
isArray
()
==
true
)
{
}
int
len
=
Array
.
getLength
(
obj
);
return
false
;
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
}
Object
o
=
Array
.
get
(
obj
,
i
);
if
(
obj
.
getClass
().
isArray
()
==
true
)
{
if
(
equals
(
o
,
element
))
{
int
len
=
Array
.
getLength
(
obj
);
return
true
;
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
}
Object
o
=
Array
.
get
(
obj
,
i
);
}
if
(
equals
(
o
,
element
))
{
}
return
true
;
return
false
;
}
}
}
}
/**
return
false
;
* 对象是否不为空(新增)
}
*
* @param obj String,List,Map,Object[],int[],long[]
/**
* @return
* 对象是否不为空(新增)
*/
*
public
static
boolean
isNotEmpty
(
Object
o
)
{
* @param obj
return
!
isEmpty
(
o
);
* String,List,Map,Object[],int[],long[]
}
* @return
*/
/**
public
static
boolean
isNotEmpty
(
Object
o
)
{
* 对象是否为空
return
!
isEmpty
(
o
);
*
}
* @param obj String,List,Map,Object[],int[],long[]
* @return
/**
*/
* 对象是否为空
@SuppressWarnings
(
"rawtypes"
)
*
public
static
boolean
isEmpty
(
Object
o
)
{
* @param obj
if
(
o
==
null
)
{
* String,List,Map,Object[],int[],long[]
return
true
;
* @return
}
*/
if
(
o
instanceof
String
)
{
@SuppressWarnings
(
"rawtypes"
)
if
(
o
.
toString
().
trim
().
equals
(
""
))
{
public
static
boolean
isEmpty
(
Object
o
)
{
return
true
;
if
(
o
==
null
)
{
}
return
true
;
}
else
if
(
o
instanceof
List
)
{
}
if
(((
List
)
o
).
size
()
==
0
)
{
if
(
o
instanceof
String
)
{
return
true
;
if
(
o
.
toString
().
trim
().
equals
(
""
))
{
}
return
true
;
}
else
if
(
o
instanceof
Map
)
{
}
if
(((
Map
)
o
).
size
()
==
0
)
{
}
else
if
(
o
instanceof
List
)
{
return
true
;
if
(((
List
)
o
).
size
()
==
0
)
{
}
return
true
;
}
else
if
(
o
instanceof
Set
)
{
}
if
(((
Set
)
o
).
size
()
==
0
)
{
}
else
if
(
o
instanceof
Map
)
{
return
true
;
if
(((
Map
)
o
).
size
()
==
0
)
{
}
return
true
;
}
else
if
(
o
instanceof
Object
[])
{
}
if
(((
Object
[])
o
).
length
==
0
)
{
}
else
if
(
o
instanceof
Set
)
{
return
true
;
if
(((
Set
)
o
).
size
()
==
0
)
{
}
return
true
;
}
else
if
(
o
instanceof
int
[])
{
}
if
(((
int
[])
o
).
length
==
0
)
{
}
else
if
(
o
instanceof
Object
[])
{
return
true
;
if
(((
Object
[])
o
).
length
==
0
)
{
}
return
true
;
}
else
if
(
o
instanceof
long
[])
{
}
if
(((
long
[])
o
).
length
==
0
)
{
}
else
if
(
o
instanceof
int
[])
{
return
true
;
if
(((
int
[])
o
).
length
==
0
)
{
}
return
true
;
}
}
return
false
;
}
else
if
(
o
instanceof
long
[])
{
}
if
(((
long
[])
o
).
length
==
0
)
{
return
true
;
/**
}
* 对象组中是否存在 Empty Object
}
*
return
false
;
* @param os 对象组
}
* @return
*/
/**
public
static
boolean
isOneEmpty
(
Object
...
os
)
{
* 对象组中是否存在 Empty Object
for
(
Object
o
:
os
)
{
*
if
(
isEmpty
(
o
))
{
* @param os
return
true
;
* 对象组
}
* @return
}
*/
return
false
;
public
static
boolean
isOneEmpty
(
Object
...
os
)
{
}
for
(
Object
o
:
os
)
{
if
(
isEmpty
(
o
))
{
/**
return
true
;
* 对象组中是否全是 Empty Object
}
*
}
* @param os
return
false
;
* @return
}
*/
public
static
boolean
isAllEmpty
(
Object
...
os
)
{
/**
for
(
Object
o
:
os
)
{
* 对象组中是否全是 Empty Object
if
(!
isEmpty
(
o
))
{
*
return
false
;
* @param os
}
* @return
}
*/
return
true
;
public
static
boolean
isAllEmpty
(
Object
...
os
)
{
}
for
(
Object
o
:
os
)
{
if
(!
isEmpty
(
o
))
{
/**
return
false
;
* 是否为数字
}
*
}
* @param obj
return
true
;
* @return
}
*/
public
static
boolean
isNum
(
Object
obj
)
{
/**
try
{
* 是否为数字
Integer
.
parseInt
(
obj
.
toString
());
*
}
catch
(
Exception
e
)
{
* @param obj
return
false
;
* @return
}
*/
return
true
;
public
static
boolean
isNum
(
Object
obj
)
{
}
try
{
Integer
.
parseInt
(
obj
.
toString
());
/**
}
catch
(
Exception
e
)
{
* 如果为空, 则调用默认值
return
false
;
*
}
* @param str
return
true
;
* @return
}
*/
public
static
Object
getValue
(
Object
str
,
Object
defaultValue
)
{
/**
if
(
isEmpty
(
str
))
{
* 如果为空, 则调用默认值
return
defaultValue
;
*
}
* @param str
return
str
;
* @return
}
*/
public
static
Object
getValue
(
Object
str
,
Object
defaultValue
)
{
/**
if
(
isEmpty
(
str
))
{
* 格式化文本
return
defaultValue
;
*
}
* @param template 文本模板,被替换的部分用 {} 表示
return
str
;
* @param values 参数值
}
* @return 格式化后的文本
*/
/**
public
static
String
format
(
String
template
,
Object
...
values
)
{
* 格式化文本
return
StrKit
.
format
(
template
,
values
);
*
}
* @param template
* 文本模板,被替换的部分用 {} 表示
/**
* @param values
* 格式化文本
* 参数值
*
* @return 格式化后的文本
* @param template 文本模板,被替换的部分用 {key} 表示
*/
* @param map 参数值对
public
static
String
format
(
String
template
,
Object
...
values
)
{
* @return 格式化后的文本
return
StrKit
.
format
(
template
,
values
);
*/
}
public
static
String
format
(
String
template
,
Map
<?,
?>
map
)
{
return
StrKit
.
format
(
template
,
map
);
/**
}
* 格式化文本
*
/**
* @param template
* 强转->string,并去掉多余空格
* 文本模板,被替换的部分用 {key} 表示
*
* @param map
* @param str
* 参数值对
* @return
* @return 格式化后的文本
*/
*/
public
static
String
toStr
(
Object
str
)
{
public
static
String
format
(
String
template
,
Map
<?,
?>
map
)
{
return
toStr
(
str
,
""
);
return
StrKit
.
format
(
template
,
map
);
}
}
/**
/**
* 强转->string,并去掉多余空格
* 强转->string,并去掉多余空格
*
*
* @param str
* @param str
* @param defaultValue
* @return
* @return
*/
*/
public
static
String
toStr
(
Object
str
)
{
public
static
String
toStr
(
Object
str
,
String
defaultValue
)
{
return
toStr
(
str
,
""
);
if
(
null
==
str
)
{
}
return
defaultValue
;
}
/**
return
str
.
toString
().
trim
();
* 强转->string,并去掉多余空格
}
*
* @param str
/**
* @param defaultValue
* 强转->int
* @return
*
*/
* @param obj
public
static
String
toStr
(
Object
str
,
String
defaultValue
)
{
* @return
if
(
null
==
str
)
{
*/
return
defaultValue
;
}
return
str
.
toString
().
trim
();
}
/**
* 强转->int
*
* @param obj
* @return
*/
// public static int toInt(Object value) {
// public static int toInt(Object value) {
// return toInt(value, -1);
// return toInt(value, -1);
// }
// }
/**
/**
* 强转->int
* 强转->int
*
*
* @param obj
* @param obj
* @param defaultValue
* @param defaultValue
* @return
* @return
*/
*/
// public static int toInt(Object value, int defaultValue) {
// public static int toInt(Object value, int defaultValue) {
// return Convert.toInt(value, defaultValue);
// return Convert.toInt(value, defaultValue);
// }
// }
/**
/**
* 强转->long
* 强转->long
*
*
* @param obj
* @param obj
* @return
* @return
*/
*/
// public static long toLong(Object value) {
// public static long toLong(Object value) {
// return toLong(value, -1);
// return toLong(value, -1);
// }
// }
/**
/**
* 强转->long
* 强转->long
*
*
* @param obj
* @param obj
* @param defaultValue
* @param defaultValue
* @return
* @return
*/
*/
// public static long toLong(Object value, long defaultValue) {
// public static long toLong(Object value, long defaultValue) {
// return Convert.toLong(value, defaultValue);
// return Convert.toLong(value, defaultValue);
// }
// }
...
@@ -423,21 +405,21 @@ public class ToolUtil {
...
@@ -423,21 +405,21 @@ public class ToolUtil {
// return URLKit.decode(url, CharsetKit.UTF_8);
// return URLKit.decode(url, CharsetKit.UTF_8);
// }
// }
/**
/**
* map的key转为小写
* map的key转为小写
*
*
* @param map
* @param map
* @return Map<String,Object>
* @return Map<String,Object>
*/
*/
public
static
Map
<
String
,
Object
>
caseInsensitiveMap
(
Map
<
String
,
Object
>
map
)
{
public
static
Map
<
String
,
Object
>
caseInsensitiveMap
(
Map
<
String
,
Object
>
map
)
{
Map
<
String
,
Object
>
tempMap
=
new
HashMap
<>();
Map
<
String
,
Object
>
tempMap
=
new
HashMap
<>();
for
(
String
key
:
map
.
keySet
())
{
for
(
String
key
:
map
.
keySet
())
{
tempMap
.
put
(
key
.
toLowerCase
(),
map
.
get
(
key
));
tempMap
.
put
(
key
.
toLowerCase
(),
map
.
get
(
key
));
}
}
return
tempMap
;
return
tempMap
;
}
}
/**
/**
* 获取map中第一个数据值
* 获取map中第一个数据值
*
*
* @param <K> Key的类型
* @param <K> Key的类型
...
@@ -456,45 +438,45 @@ public class ToolUtil {
...
@@ -456,45 +438,45 @@ public class ToolUtil {
return
obj
;
return
obj
;
}
}
/**
/**
* 创建StringBuilder对象
* 创建StringBuilder对象
*
*
* @return StringBuilder对象
* @return StringBuilder对象
*/
*/
public
static
StringBuilder
builder
(
String
...
strs
)
{
public
static
StringBuilder
builder
(
String
...
strs
)
{
final
StringBuilder
sb
=
new
StringBuilder
();
final
StringBuilder
sb
=
new
StringBuilder
();
for
(
String
str
:
strs
)
{
for
(
String
str
:
strs
)
{
sb
.
append
(
str
);
sb
.
append
(
str
);
}
}
return
sb
;
return
sb
;
}
}
/**
/**
* 创建StringBuilder对象
* 创建StringBuilder对象
*
*
* @return StringBuilder对象
* @return StringBuilder对象
*/
*/
public
static
void
builder
(
StringBuilder
sb
,
String
...
strs
)
{
public
static
void
builder
(
StringBuilder
sb
,
String
...
strs
)
{
for
(
String
str
:
strs
)
{
for
(
String
str
:
strs
)
{
sb
.
append
(
str
);
sb
.
append
(
str
);
}
}
}
}
/**
/**
* 去掉指定后缀
* 去掉指定后缀
*
*
* @param str
字符串
* @param str
字符串
* @param suffix 后缀
* @param suffix 后缀
* @return 切掉后的字符串,若后缀不是 suffix, 返回原字符串
* @return 切掉后的字符串,若后缀不是 suffix, 返回原字符串
*/
*/
public
static
String
removeSuffix
(
String
str
,
String
suffix
)
{
public
static
String
removeSuffix
(
String
str
,
String
suffix
)
{
if
(
isEmpty
(
str
)
||
isEmpty
(
suffix
))
{
if
(
isEmpty
(
str
)
||
isEmpty
(
suffix
))
{
return
str
;
return
str
;
}
}
if
(
str
.
endsWith
(
suffix
))
{
if
(
str
.
endsWith
(
suffix
))
{
return
str
.
substring
(
0
,
str
.
length
()
-
suffix
.
length
());
return
str
.
substring
(
0
,
str
.
length
()
-
suffix
.
length
());
}
}
return
str
;
return
str
;
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment