Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
E
erp-service
Project
Project
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ximai
erp-service
Commits
afa83024
Commit
afa83024
authored
Jan 15, 2025
by
李驰骋
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
统一响应结果修复
跨域问题解决
parent
59593aaf
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
39 deletions
+47
-39
ResponseWrapperAdvice.java
...sunit/scanservice/ximai/common/ResponseWrapperAdvice.java
+25
-0
UnifiedReturnConfig.java
...opsunit/scanservice/ximai/common/UnifiedReturnConfig.java
+0
-39
WebMvcConfig.java
...a/com/topsunit/scanservice/ximai/common/WebMvcConfig.java
+22
-0
No files found.
src/main/java/com/topsunit/scanservice/ximai/common/ResponseWrapperAdvice.java
0 → 100644
View file @
afa83024
package
com
.
topsunit
.
scanservice
.
ximai
.
common
;
import
org.springframework.core.MethodParameter
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.converter.HttpMessageConverter
;
import
org.springframework.http.server.ServerHttpRequest
;
import
org.springframework.http.server.ServerHttpResponse
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice
;
@ControllerAdvice
(
basePackages
=
"com.topsunit"
)
public
class
ResponseWrapperAdvice
implements
ResponseBodyAdvice
<
Object
>
{
@Override
public
boolean
supports
(
MethodParameter
methodParameter
,
Class
<?
extends
HttpMessageConverter
<?>>
aClass
)
{
return
true
;
}
@Override
public
Object
beforeBodyWrite
(
Object
o
,
MethodParameter
methodParameter
,
MediaType
mediaType
,
Class
<?
extends
HttpMessageConverter
<?>>
aClass
,
ServerHttpRequest
serverHttpRequest
,
ServerHttpResponse
serverHttpResponse
)
{
if
(
o
instanceof
ServiceResult
){
return
o
;
}
return
ServiceResult
.
ofResult
(
o
);
}
}
src/main/java/com/topsunit/scanservice/ximai/common/UnifiedReturnConfig.java
deleted
100644 → 0
View file @
59593aaf
package
com
.
topsunit
.
scanservice
.
ximai
.
common
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.MethodParameter
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.converter.HttpMessageConverter
;
import
org.springframework.http.server.ServerHttpRequest
;
import
org.springframework.http.server.ServerHttpResponse
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
org.springframework.web.servlet.config.annotation.EnableWebMvc
;
import
org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice
;
/**
* <p>Title: UnifiedReturnConfig</p>
* <p>Description: 统一消息应答</p>
*
* @author xi.feng
* @version V1.0
* @date 2021/10/19
*/
@Configuration
public
class
UnifiedReturnConfig
{
@RestControllerAdvice
(
"com.topsunit"
)
static
class
CommonResultResponseAdvice
implements
ResponseBodyAdvice
<
Object
>
{
@Override
public
boolean
supports
(
MethodParameter
methodParameter
,
Class
<?
extends
HttpMessageConverter
<?>>
aClass
)
{
return
true
;
}
@Override
public
Object
beforeBodyWrite
(
Object
o
,
MethodParameter
methodParameter
,
MediaType
mediaType
,
Class
<?
extends
HttpMessageConverter
<?>>
aClass
,
ServerHttpRequest
serverHttpRequest
,
ServerHttpResponse
serverHttpResponse
)
{
if
(
o
instanceof
ServiceResult
){
return
o
;
}
return
ServiceResult
.
ofResult
(
o
);
}
}
}
src/main/java/com/topsunit/scanservice/ximai/common/WebMvcConfig.java
View file @
afa83024
...
...
@@ -4,6 +4,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.cors.CorsConfiguration
;
import
org.springframework.web.cors.UrlBasedCorsConfigurationSource
;
import
org.springframework.web.filter.CorsFilter
;
import
org.springframework.web.servlet.LocaleResolver
;
import
org.springframework.web.servlet.config.annotation.CorsRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
...
...
@@ -20,6 +23,25 @@ public class WebMvcConfig implements WebMvcConfigurer {
.
allowedMethods
(
"GET"
,
"POST"
,
"PUT"
,
"DELETE"
)
.
maxAge
(
3600
);
}
@Bean
public
CorsFilter
corsFilter
()
{
CorsConfiguration
config
=
new
CorsConfiguration
();
config
.
setAllowCredentials
(
true
);
// 设置访问源地址
config
.
addAllowedOriginPattern
(
"*"
);
// 设置访问源请求头
config
.
addAllowedHeader
(
"*"
);
// 设置访问源请求方法
config
.
addAllowedMethod
(
"*"
);
// 有效期 1800秒
config
.
setMaxAge
(
1800L
);
// 添加映射路径,拦截一切请求
UrlBasedCorsConfigurationSource
source
=
new
UrlBasedCorsConfigurationSource
();
source
.
registerCorsConfiguration
(
"/**"
,
config
);
// 返回新的CorsFilter
return
new
CorsFilter
(
source
);
}
@Bean
public
LocaleResolver
localeResolver
(){
return
resolver
;
...
...
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