Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
J
jgjy
Project
Project
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
Graph
比较
统计图
议题
0
议题
0
列表
看板
标记
Milestones
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
注册表
注册表
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
svn
jgjy
Commits
e0497820
提交
e0497820
authored
4月 28, 2019
作者:
lvwei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
--no commit message
上级
0013af9a
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
153 行增加
和
0 行删除
+153
-0
WebAppConfig.java
...r/src/main/java/com/zrqx/commons/config/WebAppConfig.java
+98
-0
StringToDateConverter.java
.../java/com/zrqx/commons/convert/StringToDateConverter.java
+55
-0
没有找到文件。
com.zrqx.fg.member/src/main/java/com/zrqx/commons/config/WebAppConfig.java
0 → 100644
浏览文件 @
e0497820
package
com
.
zrqx
.
commons
.
config
;
import
java.nio.charset.Charset
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Set
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.convert.converter.Converter
;
import
org.springframework.format.support.FormattingConversionServiceFactoryBean
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.converter.HttpMessageConverter
;
import
org.springframework.web.bind.support.ConfigurableWebBindingInitializer
;
import
org.springframework.web.servlet.config.annotation.CorsRegistry
;
import
org.springframework.web.servlet.config.annotation.InterceptorRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
;
import
com.alibaba.fastjson.serializer.SerializerFeature
;
import
com.alibaba.fastjson.support.config.FastJsonConfig
;
import
com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter
;
import
com.zrqx.commons.convert.StringToDateConverter
;
import
com.zrqx.core.commons.interceptor.InterceptorConfig
;
import
com.zrqx.core.constant.sysuser.SysUserRequestPath
;
/**
* 使用CustomWebMvcConfigurationSupport来显对 mvc的配置
* @author lpf
* @date 2018年7月24日下午8:01:05
*/
@Configuration
public
class
WebAppConfig
extends
WebMvcConfigurerAdapter
{
/**
* 跨域支持
*/
@Override
public
void
addCorsMappings
(
CorsRegistry
registry
)
{
registry
.
addMapping
(
"/**"
).
allowedOrigins
(
"*"
).
allowedMethods
(
"*"
).
allowCredentials
(
true
).
maxAge
(
3600
);
}
@Override
public
void
addInterceptors
(
InterceptorRegistry
registry
)
{
// 注册自定义拦截器,添加拦截路径和排除拦截路径
registry
.
addInterceptor
(
new
InterceptorConfig
()).
addPathPatterns
(
SysUserRequestPath
.
BG
+
"/**"
)
.
excludePathPatterns
(
SysUserRequestPath
.
BG
+
SysUserRequestPath
.
PERMISSIONS
+
SysUserRequestPath
.
LOGIN
);
}
@Override
public
void
configureMessageConverters
(
List
<
HttpMessageConverter
<?>>
converters
)
{
converters
.
add
(
fastJsonHttpMessageConverter
());
super
.
configureMessageConverters
(
converters
);
}
@Bean
public
HttpMessageConverter
<
Object
>
fastJsonHttpMessageConverter
(){
// fastjson 转换器
FastJsonHttpMessageConverter
fast
=
new
FastJsonHttpMessageConverter
();
// fastjson 配置
FastJsonConfig
fastJsonConfig
=
new
FastJsonConfig
();
fastJsonConfig
.
setDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
fastJsonConfig
.
setCharset
(
Charset
.
forName
(
"UTF-8"
));
fastJsonConfig
.
setSerializerFeatures
(
SerializerFeature
.
WriteMapNullValue
,
SerializerFeature
.
WriteDateUseDateFormat
,
SerializerFeature
.
QuoteFieldNames
);
fast
.
setFastJsonConfig
(
fastJsonConfig
);
// fastJson 支持解析类型
List
<
MediaType
>
list
=
new
ArrayList
<
MediaType
>();
list
.
add
(
MediaType
.
APPLICATION_JSON_UTF8
);
fast
.
setSupportedMediaTypes
(
list
);
return
fast
;
}
@Bean
public
Converter
<
String
,
Date
>
stringToDateConverter
()
{
StringToDateConverter
conver
=
new
StringToDateConverter
();
return
conver
;
}
@Bean
public
FormattingConversionServiceFactoryBean
conversionService
()
{
FormattingConversionServiceFactoryBean
bean
=
new
FormattingConversionServiceFactoryBean
();
Set
<
Converter
<?,
?>>
set
=
new
HashSet
<
Converter
<?,
?>>();
set
.
add
(
stringToDateConverter
());
bean
.
setConverters
(
set
);
return
bean
;
}
public
ConfigurableWebBindingInitializer
webBindingInitializer
(
@Autowired
ConfigurableWebBindingInitializer
config
)
{
config
.
setConversionService
(
conversionService
().
getObject
());
return
config
;
}
}
\ No newline at end of file
com.zrqx.fg.member/src/main/java/com/zrqx/commons/convert/StringToDateConverter.java
0 → 100644
浏览文件 @
e0497820
package
com
.
zrqx
.
commons
.
convert
;
import
java.text.DateFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
import
org.springframework.core.convert.converter.Converter
;
/**
* 字符串转换为Date
* @author lpf
* @date 2018年7月10日下午4:02:01
*/
public
class
StringToDateConverter
implements
Converter
<
String
,
Date
>
{
private
static
final
List
<
String
>
formarts
=
new
ArrayList
<
String
>(
4
);
static
{
formarts
.
add
(
"yyyy-MM"
);
formarts
.
add
(
"yyyy-MM-dd"
);
formarts
.
add
(
"yyyy-MM-dd hh:mm"
);
formarts
.
add
(
"yyyy-MM-dd HH:mm:ss"
);
}
@Override
public
Date
convert
(
String
source
)
{
String
value
=
source
.
trim
();
if
(
""
.
equals
(
value
))
{
return
null
;
}
if
(
source
.
matches
(
"^\\d{4}-\\d{1,2}$"
)){
return
parseDate
(
source
,
formarts
.
get
(
0
));
}
else
if
(
source
.
matches
(
"^\\d{4}-\\d{1,2}-\\d{1,2}$"
)){
return
parseDate
(
source
,
formarts
.
get
(
1
));
}
else
if
(
source
.
matches
(
"^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$"
)){
return
parseDate
(
source
,
formarts
.
get
(
2
));
}
else
if
(
source
.
matches
(
"^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$"
)){
return
parseDate
(
source
,
formarts
.
get
(
3
));
}
else
{
throw
new
IllegalArgumentException
(
"Invalid boolean value '"
+
source
+
"'"
);
}
}
public
Date
parseDate
(
String
dateStr
,
String
format
)
{
Date
date
=
null
;
try
{
DateFormat
dateFormat
=
new
SimpleDateFormat
(
format
);
date
=
(
Date
)
dateFormat
.
parse
(
dateStr
);
}
catch
(
Exception
e
)
{
}
return
date
;
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论