Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
architecture
/
glsx-archetype
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 620e100e
authored
Apr 17, 2023
by
刘宇峰
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
框架完善
1 parent
bf54776c
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
151 additions
and
8 deletions
rest-archetype-framework/rest-admin-core/src/main/java/com/glsx/plat/core/web/OpenApiResult.java
rest-archetype-framework/rest-admin-exception/src/main/java/com/glsx/plat/exception/OpenApiResultException.java
rest-archetype-framework/rest-admin-exception/src/main/java/com/glsx/plat/exception/ServiceException.java
rest-archetype-framework/rest-admin-gateway-server/pom.xml
rest-archetype-framework/rest-admin-web/pom.xml
rest-archetype-framework/rest-admin-web/src/main/java/com/glsx/plat/web/advice/ResponseResultBodyAdvice.java
rest-archetype-framework/rest-admin-core/src/main/java/com/glsx/plat/core/web/OpenApiResult.java
0 → 100644
View file @
620e100
package
com
.
glsx
.
plat
.
core
.
web
;
import
lombok.AllArgsConstructor
;
import
lombok.Builder
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
lombok.experimental.Accessors
;
import
java.io.Serializable
;
@Data
@Builder
@Accessors
(
chain
=
true
)
@NoArgsConstructor
@AllArgsConstructor
public
class
OpenApiResult
<
T
>
implements
Serializable
{
private
static
final
String
SUCCESS_DEFAULT_CODE
=
"00000"
;
private
static
final
String
FAILURE_DEFAULT_CODE
=
"44444"
;
private
Boolean
success
;
private
String
code
;
private
String
desc
;
private
T
result
;
private
Long
time
;
public
static
<
T
>
OpenApiResult
<
T
>
success
(
T
result
)
{
return
OpenApiResult
.<
T
>
builder
()
.
code
(
SUCCESS_DEFAULT_CODE
)
.
success
(
true
)
.
desc
(
"成功"
)
.
result
(
result
)
.
build
();
}
public
static
<
T
>
OpenApiResult
<
T
>
failure
(
String
code
,
String
desc
)
{
return
OpenApiResult
.<
T
>
builder
()
.
success
(
true
)
.
code
(
code
)
.
desc
(
desc
)
.
result
(
null
)
.
build
();
}
}
rest-archetype-framework/rest-admin-exception/src/main/java/com/glsx/plat/exception/OpenApiResultException.java
0 → 100644
View file @
620e100
package
com
.
glsx
.
plat
.
exception
;
import
com.glsx.plat.core.web.OpenApiResult
;
public
class
OpenApiResultException
extends
RuntimeException
{
protected
OpenApiResult
r
;
protected
static
final
int
MIN_AUTO_CODE
=
40000
;
protected
OpenApiResultException
(
OpenApiResultException
parent
)
{
if
(
parent
!=
null
)
{
this
.
r
=
parent
.
r
;
}
}
/**
* 内部系统用,对外api接口不能使用随机错误码
*
* @param message
* @return
*/
public
static
OpenApiResultException
create
(
String
message
)
{
int
value
=
(
int
)
(
MIN_AUTO_CODE
+
Math
.
round
((
Integer
.
MAX_VALUE
-
MIN_AUTO_CODE
)
*
Math
.
random
()));
return
create
(
value
,
message
);
}
/**
* 返回码为整型
*
* @param returnCode
* @param message
* @return
*/
public
static
OpenApiResultException
create
(
int
returnCode
,
String
message
)
{
OpenApiResultException
businessException
=
new
OpenApiResultException
(
null
);
businessException
.
r
=
OpenApiResult
.
failure
(
String
.
valueOf
(
returnCode
),
message
);
return
businessException
;
}
/**
* 返回码可以为字符串型
*
* @param returnCode
* @param message
* @return
*/
public
static
OpenApiResultException
create
(
String
returnCode
,
String
message
)
{
OpenApiResultException
businessException
=
new
OpenApiResultException
(
null
);
businessException
.
r
=
OpenApiResult
.
failure
(
returnCode
,
message
);
return
businessException
;
}
public
static
OpenApiResultException
create
(
OpenApiResult
r
)
{
OpenApiResultException
businessException
=
new
OpenApiResultException
(
null
);
businessException
.
r
=
r
;
return
businessException
;
}
@Override
public
String
getMessage
()
{
return
r
.
getDesc
();
}
public
OpenApiResult
getResultEntity
()
{
return
r
;
}
}
rest-archetype-framework/rest-admin-exception/src/main/java/com/glsx/plat/exception/ServiceException.java
View file @
620e100
...
@@ -20,7 +20,7 @@ public class ServiceException extends RuntimeException implements Serializable {
...
@@ -20,7 +20,7 @@ public class ServiceException extends RuntimeException implements Serializable {
private
String
code
;
private
String
code
;
public
static
ServiceException
create
(
String
code
,
String
msg
)
{
public
static
ServiceException
create
(
String
code
,
String
msg
)
{
log
.
error
(
"RuntimeException {} {}"
,
code
,
msg
);
log
.
error
(
"
Service
RuntimeException {} {}"
,
code
,
msg
);
ServiceException
exception
=
new
ServiceException
();
ServiceException
exception
=
new
ServiceException
();
exception
.
setCode
(
code
);
exception
.
setCode
(
code
);
exception
.
setMsg
(
msg
);
exception
.
setMsg
(
msg
);
...
...
rest-archetype-framework/rest-admin-gateway-server/pom.xml
View file @
620e100
...
@@ -17,10 +17,6 @@
...
@@ -17,10 +17,6 @@
<artifactId>
spring-cloud-starter-gateway
</artifactId>
<artifactId>
spring-cloud-starter-gateway
</artifactId>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-loadbalancer
</artifactId>
</dependency>
<dependency>
<groupId>
com.glsx
</groupId>
<groupId>
com.glsx
</groupId>
<artifactId>
rest-admin-config
</artifactId>
<artifactId>
rest-admin-config
</artifactId>
</dependency>
</dependency>
...
...
rest-archetype-framework/rest-admin-web/pom.xml
View file @
620e100
...
@@ -47,6 +47,10 @@
...
@@ -47,6 +47,10 @@
</exclusions>
</exclusions>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-validation
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework
</groupId>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-context
</artifactId>
<artifactId>
spring-context
</artifactId>
</dependency>
</dependency>
...
...
rest-archetype-framework/rest-admin-web/src/main/java/com/glsx/plat/web/advice/ResponseResultBodyAdvice.java
View file @
620e100
...
@@ -2,7 +2,9 @@ package com.glsx.plat.web.advice;
...
@@ -2,7 +2,9 @@ package com.glsx.plat.web.advice;
import
cn.hutool.json.JSONUtil
;
import
cn.hutool.json.JSONUtil
;
import
com.glsx.plat.core.annotation.ResponseResultBody
;
import
com.glsx.plat.core.annotation.ResponseResultBody
;
import
com.glsx.plat.core.web.OpenApiResult
;
import
com.glsx.plat.core.web.Result
;
import
com.glsx.plat.core.web.Result
;
import
com.glsx.plat.exception.OpenApiResultException
;
import
com.glsx.plat.exception.ResultException
;
import
com.glsx.plat.exception.ResultException
;
import
com.glsx.plat.exception.ServiceException
;
import
com.glsx.plat.exception.ServiceException
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
...
@@ -45,6 +47,9 @@ public class ResponseResultBodyAdvice implements ResponseBodyAdvice<Object> {
...
@@ -45,6 +47,9 @@ public class ResponseResultBodyAdvice implements ResponseBodyAdvice<Object> {
if
(
body
instanceof
Result
)
{
if
(
body
instanceof
Result
)
{
return
body
;
return
body
;
}
}
if
(
body
instanceof
OpenApiResult
)
{
return
body
;
}
if
(
body
instanceof
String
)
{
if
(
body
instanceof
String
)
{
return
JSONUtil
.
toJsonStr
(
Result
.
success
(
body
));
return
JSONUtil
.
toJsonStr
(
Result
.
success
(
body
));
}
}
...
@@ -73,12 +78,27 @@ public class ResponseResultBodyAdvice implements ResponseBodyAdvice<Object> {
...
@@ -73,12 +78,27 @@ public class ResponseResultBodyAdvice implements ResponseBodyAdvice<Object> {
return
this
.
handleException
(
ex
,
headers
,
request
);
return
this
.
handleException
(
ex
,
headers
,
request
);
}
}
@ExceptionHandler
(
OpenApiResultException
.
class
)
public
final
ResponseEntity
<
OpenApiResult
<?>>
openApiResultExceptionHandler
(
Exception
ex
,
WebRequest
request
)
{
log
.
error
(
"OpenApiResultExceptionHandler: {}"
,
ex
.
getMessage
());
ex
.
printStackTrace
();
return
this
.
handleResultException
((
OpenApiResultException
)
ex
,
new
HttpHeaders
(),
request
);
}
/**
/**
* 对ResultException类返回返回结果的处理
* 对ResultException类返回返回结果的处理
*/
*/
protected
ResponseEntity
<
Result
<?>>
handleResultException
(
ResultException
ex
,
HttpHeaders
headers
,
WebRequest
request
)
{
protected
ResponseEntity
<
Result
<?>>
handleResultException
(
ResultException
ex
,
HttpHeaders
headers
,
WebRequest
request
)
{
Result
<?>
body
=
ex
.
getResultEntity
();
Result
<?>
body
=
ex
.
getResultEntity
();
return
this
.
handleExceptionInternal
(
ex
,
body
,
headers
,
HttpStatus
.
INTERNAL_SERVER_ERROR
,
request
);
return
this
.
handleResultExceptionInternal
(
ex
,
body
,
headers
,
HttpStatus
.
INTERNAL_SERVER_ERROR
,
request
);
}
/**
* 对ResultException类返回返回结果的处理
*/
protected
ResponseEntity
<
OpenApiResult
<?>>
handleResultException
(
OpenApiResultException
ex
,
HttpHeaders
headers
,
WebRequest
request
)
{
OpenApiResult
<?>
body
=
ex
.
getResultEntity
();
return
this
.
handleOpenApiResultExceptionInternal
(
ex
,
body
,
headers
,
HttpStatus
.
INTERNAL_SERVER_ERROR
,
request
);
}
}
/**
/**
...
@@ -88,7 +108,7 @@ public class ResponseResultBodyAdvice implements ResponseBodyAdvice<Object> {
...
@@ -88,7 +108,7 @@ public class ResponseResultBodyAdvice implements ResponseBodyAdvice<Object> {
//返回异常消息
//返回异常消息
HttpStatus
status
=
HttpStatus
.
INTERNAL_SERVER_ERROR
;
HttpStatus
status
=
HttpStatus
.
INTERNAL_SERVER_ERROR
;
Result
<?>
body
=
new
Result
(
status
.
value
(),
ex
.
getMessage
());
Result
<?>
body
=
new
Result
(
status
.
value
(),
ex
.
getMessage
());
return
this
.
handleExceptionInternal
(
ex
,
body
,
headers
,
status
,
request
);
return
this
.
handle
Result
ExceptionInternal
(
ex
,
body
,
headers
,
status
,
request
);
}
}
/**
/**
...
@@ -99,12 +119,21 @@ public class ResponseResultBodyAdvice implements ResponseBodyAdvice<Object> {
...
@@ -99,12 +119,21 @@ public class ResponseResultBodyAdvice implements ResponseBodyAdvice<Object> {
* request attribute and creates a {@link ResponseEntity} from the given
* request attribute and creates a {@link ResponseEntity} from the given
* body, headers, and status.
* body, headers, and status.
*/
*/
protected
ResponseEntity
<
Result
<?>>
handleExceptionInternal
(
protected
ResponseEntity
<
Result
<?>>
handle
Result
ExceptionInternal
(
Exception
ex
,
Result
<?>
body
,
HttpHeaders
headers
,
HttpStatus
status
,
WebRequest
request
)
{
Exception
ex
,
Result
<?>
body
,
HttpHeaders
headers
,
HttpStatus
status
,
WebRequest
request
)
{
if
(
HttpStatus
.
INTERNAL_SERVER_ERROR
.
equals
(
status
))
{
request
.
setAttribute
(
WebUtils
.
ERROR_EXCEPTION_ATTRIBUTE
,
ex
,
WebRequest
.
SCOPE_REQUEST
);
}
return
new
ResponseEntity
<>(
body
,
headers
,
status
);
}
protected
ResponseEntity
<
OpenApiResult
<?>>
handleOpenApiResultExceptionInternal
(
Exception
ex
,
OpenApiResult
<?>
body
,
HttpHeaders
headers
,
HttpStatus
status
,
WebRequest
request
)
{
if
(
HttpStatus
.
INTERNAL_SERVER_ERROR
.
equals
(
status
))
{
if
(
HttpStatus
.
INTERNAL_SERVER_ERROR
.
equals
(
status
))
{
request
.
setAttribute
(
WebUtils
.
ERROR_EXCEPTION_ATTRIBUTE
,
ex
,
WebRequest
.
SCOPE_REQUEST
);
request
.
setAttribute
(
WebUtils
.
ERROR_EXCEPTION_ATTRIBUTE
,
ex
,
WebRequest
.
SCOPE_REQUEST
);
}
}
return
new
ResponseEntity
<>(
body
,
headers
,
status
);
return
new
ResponseEntity
<>(
body
,
headers
,
status
);
}
}
}
}
Write
Preview
Markdown
is supported
Attach a file
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 post a comment