Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
nrgl
Project
Project
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
Graph
比较
统计图
议题
0
议题
0
列表
看板
标记
Milestones
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
注册表
注册表
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
svn
nrgl
Commits
93beb9eb
提交
93beb9eb
authored
2月 11, 2019
作者:
niguanghui
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
--no commit message
上级
be8c072c
隐藏空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
420 行增加
和
0 行删除
+420
-0
Swagger2Config.java
...in/java/com/zrqx/works/commons/config/Swagger2Config.java
+70
-0
WebAppConfig.java
...main/java/com/zrqx/works/commons/config/WebAppConfig.java
+27
-0
CustomExceptionHandler.java
...rqx/works/commons/interceptor/CustomExceptionHandler.java
+87
-0
InterceptorConfig.java
...com/zrqx/works/commons/interceptor/InterceptorConfig.java
+51
-0
LogAspect.java
...in/java/com/zrqx/works/commons/interceptor/LogAspect.java
+33
-0
TxAdviceInterceptor.java
...m/zrqx/works/commons/interceptor/TxAdviceInterceptor.java
+77
-0
Copy.java
...works/src/main/java/com/zrqx/works/commons/util/Copy.java
+59
-0
SysuserUtil.java
...rc/main/java/com/zrqx/works/commons/util/SysuserUtil.java
+16
-0
没有找到文件。
com.zrqx.works/src/main/java/com/zrqx/works/commons/config/Swagger2Config.java
0 → 100644
浏览文件 @
93beb9eb
package
com
.
zrqx
.
works
.
commons
.
config
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.bind.annotation.CrossOrigin
;
import
springfox.documentation.builders.ApiInfoBuilder
;
import
springfox.documentation.builders.PathSelectors
;
import
springfox.documentation.builders.RequestHandlerSelectors
;
import
springfox.documentation.service.ApiInfo
;
import
springfox.documentation.service.Contact
;
import
springfox.documentation.spi.DocumentationType
;
import
springfox.documentation.spring.web.plugins.Docket
;
import
springfox.documentation.swagger2.annotations.EnableSwagger2
;
/**
* 访问地址
* http://ip:port/swagger-ui.html
最常用的5个注解
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiModelProperty:用对象接收参数时,描述对象的一个字段
其它若干
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiClass
@ApiError
@ApiErrors
@ApiParamImplicit
@ApiParamsImplicit
*/
@EnableSwagger2
@Configuration
public
class
Swagger2Config
{
@Bean
public
Docket
createRestApi
()
{
return
new
Docket
(
DocumentationType
.
SWAGGER_2
)
.
apiInfo
(
apiInfo
())
.
select
()
//为当前包路径
.
apis
(
RequestHandlerSelectors
.
basePackage
(
"com.zrqx"
))
.
paths
(
PathSelectors
.
any
())
.
build
();
}
//构建 api文档的详细信息函数
private
ApiInfo
apiInfo
()
{
return
new
ApiInfoBuilder
()
//页面标题
.
title
(
"sysUser 测试使用 Swagger2 构建RESTful API"
)
//描述
.
description
(
"sysUser服务 API 描述"
)
//创建人
.
contact
(
new
Contact
(
"陈新昌"
,
"www.baidu.com"
,
"cxinchang@126.com"
))
//版本号
.
version
(
"3.0"
)
.
build
();
}
}
\ No newline at end of file
com.zrqx.works/src/main/java/com/zrqx/works/commons/config/WebAppConfig.java
0 → 100644
浏览文件 @
93beb9eb
package
com
.
zrqx
.
works
.
commons
.
config
;
import
org.springframework.context.annotation.Configuration
;
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.zrqx.core.constant.sysuser.SysUserRequestPath
;
import
com.zrqx.works.commons.interceptor.InterceptorConfig
;
@Configuration
public
class
WebAppConfig
extends
WebMvcConfigurerAdapter
{
/**
* 跨域支持
*/
@Override
public
void
addCorsMappings
(
CorsRegistry
registry
)
{
registry
.
addMapping
(
"/**"
).
allowedOrigins
(
"*"
).
allowedMethods
(
"*"
).
allowCredentials
(
false
).
maxAge
(
3600
);
}
@Override
public
void
addInterceptors
(
InterceptorRegistry
registry
)
{
//注册自定义拦截器,添加拦截路径和排除拦截路径
registry
.
addInterceptor
(
new
InterceptorConfig
()).
addPathPatterns
(
SysUserRequestPath
.
BG
+
"/**"
)
.
excludePathPatterns
(
SysUserRequestPath
.
BG
+
SysUserRequestPath
.
PERMISSIONS
+
SysUserRequestPath
.
LOGIN
);
}
}
\ No newline at end of file
com.zrqx.works/src/main/java/com/zrqx/works/commons/interceptor/CustomExceptionHandler.java
0 → 100644
浏览文件 @
93beb9eb
package
com
.
zrqx
.
works
.
commons
.
interceptor
;
import
java.util.Date
;
import
java.util.List
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
org.apache.log4j.Logger
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.validation.BindException
;
import
org.springframework.validation.ObjectError
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
import
com.zrqx.core.exception.BaseException
;
import
com.zrqx.core.util.CallBack
;
/**
*controller 异常处理
* @ClassName: CustomExceptionHandler
* @Description: TODO(这里用一句话描述这个类的作用)
* @author 杨振广
* @date 2016-7-14 上午9:45:40
*
*/
@ControllerAdvice
public
class
CustomExceptionHandler
{
private
static
Logger
logger
=
Logger
.
getLogger
(
CustomExceptionHandler
.
class
);
@ExceptionHandler
(
Exception
.
class
)
@ResponseStatus
(
HttpStatus
.
OK
)
@ResponseBody
public
Object
handlerException
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Exception
e
){
e
.
printStackTrace
();
StackTraceElement
[]
s
=
e
.
getStackTrace
();
StringBuffer
stes
=
new
StringBuffer
(
s
.
length
*
2
);
for
(
StackTraceElement
stackTraceElement
:
s
)
{
stes
.
append
(
stackTraceElement
.
toString
());
stes
.
append
(
"<br/>"
);
}
String
info
=
"异常时间:"
+
new
Date
().
toLocaleString
()+
"\n请求地址:"
+
request
.
getRequestURI
()+
"\n参数:"
+
request
.
getQueryString
()+
"\n"
+
e
.
getMessage
();
logger
.
error
(
info
+
"\n"
+
stes
.
toString
());
request
.
setAttribute
(
"error"
,
info
);
request
.
setAttribute
(
"errorInfo"
,
e
.
getStackTrace
());
return
CallBack
.
exception
(
e
.
getMessage
());
}
/**
* 参数验证异常处理
* @Title: handlerException
* @Description:
* @param request
* @param response
* @param e BindException
* @return
* @author lpf
* @date: 2018年5月9日 下午5:01:58
*/
@ExceptionHandler
(
BindException
.
class
)
@ResponseStatus
(
HttpStatus
.
OK
)
@ResponseBody
public
Object
handlerException
(
HttpServletRequest
request
,
HttpServletResponse
response
,
BindException
e
)
{
List
<
ObjectError
>
errors
=
e
.
getAllErrors
();
StringBuffer
msg
=
new
StringBuffer
();
for
(
ObjectError
objectError
:
errors
)
{
msg
.
append
(
objectError
.
getDefaultMessage
());
msg
.
append
(
","
);
}
msg
.
deleteCharAt
(
msg
.
length
()-
1
);
return
CallBack
.
validate
(
msg
.
toString
());
}
/**
* 执行异常处理
* @Description:
* @param e BaseException
* @return
* @date: 2018年5月9日 下午5:01:58
*
*/
@ExceptionHandler
(
BaseException
.
class
)
@ResponseStatus
(
HttpStatus
.
OK
)
@ResponseBody
public
Object
myException
(
BaseException
e
)
{
return
CallBack
.
exception
(
e
.
getCode
(),
null
,
e
.
getMessage
());
}
}
\ No newline at end of file
com.zrqx.works/src/main/java/com/zrqx/works/commons/interceptor/InterceptorConfig.java
0 → 100644
浏览文件 @
93beb9eb
package
com
.
zrqx
.
works
.
commons
.
interceptor
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpSession
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.web.servlet.HandlerInterceptor
;
import
org.springframework.web.servlet.ModelAndView
;
public
class
InterceptorConfig
implements
HandlerInterceptor
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
InterceptorConfig
.
class
);
/**
* 进入controller层之前拦截请求
* @param httpServletRequest
* @param httpServletResponse
* @param o
* @return
* @throws Exception
*/
@Override
public
boolean
preHandle
(
HttpServletRequest
httpServletRequest
,
HttpServletResponse
httpServletResponse
,
Object
o
)
throws
Exception
{
HttpSession
session
=
httpServletRequest
.
getSession
();
if
(
session
.
getAttribute
(
"user"
)
!=
null
){
return
true
;
}
else
{
// PrintWriter printWriter = httpServletResponse.getWriter();
// printWriter.write(JsonUtil.bean2Json(CallBack.create(ResponseCodeEnum.NO_LOGIN)));
// return false;
return
true
;
}
}
@Override
public
void
postHandle
(
HttpServletRequest
httpServletRequest
,
HttpServletResponse
httpServletResponse
,
Object
o
,
ModelAndView
modelAndView
)
throws
Exception
{
log
.
info
(
"--------------处理请求完成后视图渲染之前的处理操作---------------"
);
}
@Override
public
void
afterCompletion
(
HttpServletRequest
httpServletRequest
,
HttpServletResponse
httpServletResponse
,
Object
o
,
Exception
e
)
throws
Exception
{
log
.
info
(
"---------------视图渲染之后的操作--------------------------"
);
}
}
\ No newline at end of file
com.zrqx.works/src/main/java/com/zrqx/works/commons/interceptor/LogAspect.java
0 → 100644
浏览文件 @
93beb9eb
package
com
.
zrqx
.
works
.
commons
.
interceptor
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
com.zrqx.core.model.sysuser.log.Log
;
import
com.zrqx.core.util.LogUtils
;
import
com.zrqx.works.bg.client.sysuser.SysuserClient
;
import
com.zrqx.works.commons.util.SysuserUtil
;
import
io.swagger.annotations.ApiOperation
;
@Aspect
@Component
public
class
LogAspect
{
@Autowired
private
SysuserClient
logService
;
// 环绕通知,环绕增强,相当于MethodInterceptor
@Around
(
value
=
"execution(* com.zrqx.content.bg.controller..*(..)) and @annotation(annotation) and @annotation(org.springframework.web.bind.annotation.PostMapping)"
,
argNames
=
"annotation"
)
public
Object
arround
(
ProceedingJoinPoint
pjp
,
ApiOperation
annotation
)
throws
Throwable
{
Log
log
=
LogUtils
.
getLog
(
pjp
,
annotation
,
this
.
getClass
(),
SysuserUtil
.
getSysuserAccount
(),
SysuserUtil
.
getSysuserIP
());
logService
.
saveLog
(
log
);
return
log
.
getResult
();
}
}
\ No newline at end of file
com.zrqx.works/src/main/java/com/zrqx/works/commons/interceptor/TxAdviceInterceptor.java
0 → 100644
浏览文件 @
93beb9eb
package
com
.
zrqx
.
works
.
commons
.
interceptor
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.Map
;
import
javax.sql.DataSource
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.springframework.aop.Advisor
;
import
org.springframework.aop.aspectj.AspectJExpressionPointcut
;
import
org.springframework.aop.support.DefaultPointcutAdvisor
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.jdbc.datasource.DataSourceTransactionManager
;
import
org.springframework.transaction.PlatformTransactionManager
;
import
org.springframework.transaction.TransactionDefinition
;
import
org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource
;
import
org.springframework.transaction.interceptor.RollbackRuleAttribute
;
import
org.springframework.transaction.interceptor.RuleBasedTransactionAttribute
;
import
org.springframework.transaction.interceptor.TransactionAttribute
;
import
org.springframework.transaction.interceptor.TransactionInterceptor
;
/**
* Created by guozp on 2017/8/28.
*/
@Aspect
//@Component 事务依然生效
@Configuration
public
class
TxAdviceInterceptor
{
private
static
final
int
TX_METHOD_TIMEOUT
=
5
;
private
static
final
String
AOP_POINTCUT_EXPRESSION
=
"execution(* com.zrqx..service..*Impl.*(..))"
;
@Autowired
private
PlatformTransactionManager
transactionManager
;
// 其中 dataSource 框架会自动为我们注入
@Bean
(
name
=
"transactionManager"
)
public
PlatformTransactionManager
transactionManager
(
DataSource
dataSource
)
{
return
new
DataSourceTransactionManager
(
dataSource
);
}
@Bean
public
TransactionInterceptor
txAdvice
()
{
NameMatchTransactionAttributeSource
source
=
new
NameMatchTransactionAttributeSource
();
/*只读事务,不做更新操作*/
RuleBasedTransactionAttribute
readOnlyTx
=
new
RuleBasedTransactionAttribute
();
readOnlyTx
.
setReadOnly
(
true
);
readOnlyTx
.
setPropagationBehavior
(
TransactionDefinition
.
PROPAGATION_REQUIRED
);
/*当前存在事务就使用当前事务,当前不存在事务就创建一个新的事务*/
RuleBasedTransactionAttribute
requiredTx
=
new
RuleBasedTransactionAttribute
();
requiredTx
.
setRollbackRules
(
Collections
.
singletonList
(
new
RollbackRuleAttribute
(
Exception
.
class
)));
requiredTx
.
setPropagationBehavior
(
TransactionDefinition
.
PROPAGATION_REQUIRED
);
requiredTx
.
setTimeout
(
TX_METHOD_TIMEOUT
);
Map
<
String
,
TransactionAttribute
>
txMap
=
new
HashMap
<>();
txMap
.
put
(
"add*"
,
requiredTx
);
txMap
.
put
(
"save*"
,
requiredTx
);
txMap
.
put
(
"insert*"
,
requiredTx
);
txMap
.
put
(
"update*"
,
requiredTx
);
txMap
.
put
(
"delete*"
,
requiredTx
);
txMap
.
put
(
"get*"
,
readOnlyTx
);
txMap
.
put
(
"query*"
,
readOnlyTx
);
source
.
setNameMap
(
txMap
);
TransactionInterceptor
txAdvice
=
new
TransactionInterceptor
(
transactionManager
,
source
);
return
txAdvice
;
}
@Bean
public
Advisor
txAdviceAdvisor
()
{
AspectJExpressionPointcut
pointcut
=
new
AspectJExpressionPointcut
();
pointcut
.
setExpression
(
AOP_POINTCUT_EXPRESSION
);
return
new
DefaultPointcutAdvisor
(
pointcut
,
txAdvice
());
//return new DefaultPointcutAdvisor(pointcut, txAdvice);
}
}
\ No newline at end of file
com.zrqx.works/src/main/java/com/zrqx/works/commons/util/Copy.java
0 → 100644
浏览文件 @
93beb9eb
package
com
.
zrqx
.
works
.
commons
.
util
;
import
java.util.List
;
import
java.util.function.Predicate
;
import
java.util.stream.Collectors
;
import
org.springframework.beans.BeanUtils
;
import
com.zrqx.core.exception.BaseException
;
public
class
Copy
{
/**
* 批量复制对象
*
* @param list
* @param clazz
* @return
*/
public
static
<
VO
,
R
>
List
<
VO
>
copyList
(
List
<
R
>
list
,
Class
<
VO
>
clazz
)
{
return
list
.
stream
().
map
(
obj
->
{
VO
vo
=
null
;
try
{
vo
=
clazz
.
newInstance
();
BeanUtils
.
copyProperties
(
obj
,
vo
);
}
catch
(
InstantiationException
|
IllegalAccessException
e
)
{
e
.
printStackTrace
();
throw
new
BaseException
(
"批量复制对象时创建对象失败。"
);
}
return
vo
;
}).
collect
(
Collectors
.
toList
());
}
/**
* 过滤批量复制对象
*
* @param list
* @param clazz
* @param filter
* @return
*/
public
static
<
VO
,
R
>
List
<
VO
>
copyList
(
List
<
R
>
list
,
Class
<
VO
>
clazz
,
Predicate
<
R
>
filter
)
{
return
list
.
stream
().
filter
(
filter
).
map
(
obj
->
{
VO
vo
=
null
;
try
{
vo
=
clazz
.
newInstance
();
BeanUtils
.
copyProperties
(
obj
,
vo
);
}
catch
(
InstantiationException
|
IllegalAccessException
e
)
{
e
.
printStackTrace
();
throw
new
BaseException
(
"批量复制对象时创建对象失败。"
);
}
return
vo
;
}).
collect
(
Collectors
.
toList
());
}
}
com.zrqx.works/src/main/java/com/zrqx/works/commons/util/SysuserUtil.java
0 → 100644
浏览文件 @
93beb9eb
package
com
.
zrqx
.
works
.
commons
.
util
;
public
class
SysuserUtil
{
/**
* 获取当前登录的系统管理员账号
*/
public
static
String
getSysuserAccount
(){
return
"test"
;
}
/**
* 获取当前登录的系统管理员的ip
*/
public
static
String
getSysuserIP
(){
return
"127.0.0.1"
;
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论