Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
open-source
/
chejia-smart-admin
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 c00509e4
authored
May 10, 2021
by
tyfanchz
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
增加 feign 解码器,用于解析服务方返回的 R 中的数据
1 parent
ea8d1bd1
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
381 additions
and
18 deletions
src/main/java/com/glsx/admin/config/FeignDecoder.java
src/main/java/com/glsx/admin/config/FeignDecoderConfig.java
src/main/java/com/glsx/admin/config/FeignDecoderForR.java
src/main/java/com/glsx/admin/remote/IndexFeignClient.java
src/main/java/com/glsx/admin/remote/OrderValueFeignClient.java
src/main/java/com/glsx/admin/remote/ProductConditionFeignClient.java
src/main/java/com/glsx/admin/remote/ProductContentFeignClient.java
src/main/java/com/glsx/admin/remote/ProductFeignClient.java
src/main/java/com/glsx/admin/remote/ProductTypeFeignClient.java
src/main/java/com/glsx/admin/remote/SceneTemplateFeignClient.java
src/main/java/com/glsx/admin/remote/SysCodeFeignClient.java
src/main/java/com/glsx/admin/config/FeignDecoder.java
0 → 100644
View file @
c00509e
package
com
.
glsx
.
admin
.
config
;
import
java.io.IOException
;
import
java.lang.reflect.ParameterizedType
;
import
java.lang.reflect.Type
;
import
java.nio.charset.StandardCharsets
;
import
cn.hutool.core.io.IoUtil
;
import
com.alibaba.fastjson.JSON
;
import
com.glsx.admin.exception.BusinessException
;
import
com.glsx.admin.exception.ResultEnum
;
import
feign.Response
;
import
feign.codec.Decoder
;
import
lombok.extern.slf4j.Slf4j
;
/**
* <p>Description: feign 自定义解码器
* <p>Project: chejia-smart
*
* @param <T> 方法出参类型
* @author wbh
* @date 2021-05-08
*/
@Slf4j
public
abstract
class
FeignDecoder
<
T
>
implements
Decoder
{
@Override
public
Object
decode
(
Response
response
,
Type
type
)
{
Object
result
;
try
{
if
(
response
.
status
()
>=
200
&&
response
.
status
()
<
300
)
{
// 正常返回,解析返回对象
result
=
this
.
handleNormalResponse
(
response
,
type
);
}
else
{
// 非正常返回,抛出异常
this
.
handleErrorResponse
(
response
);
result
=
null
;
}
}
catch
(
BusinessException
e
)
{
throw
e
;
}
catch
(
Exception
e
)
{
throw
BusinessException
.
create
(
ResultEnum
.
REMOTE_SERVICE_ERROR
);
}
return
result
;
}
/**
* 处理正常返回,即 HTTP 状态码为 2xx
*
* @param response feign 请求
* @param type 方法出参类型
* @return 结果
* @throws IOException 异常
*/
private
Object
handleNormalResponse
(
Response
response
,
Type
type
)
throws
IOException
{
Object
result
;
Response
.
Body
body
=
response
.
body
();
if
(
body
==
null
)
{
result
=
null
;
}
else
{
String
respJson
=
IoUtil
.
read
(
body
.
asReader
(
StandardCharsets
.
UTF_8
));
Type
responseType
=
this
.
getResponseType
();
// 响应对象
T
resp
=
JSON
.
parseObject
(
respJson
,
responseType
);
// 判断出参类型和响应对象类型是否匹配,即响应对象要能赋值给方法出参
boolean
methodTypeMatched
=
type
.
getTypeName
().
equals
(
responseType
.
getTypeName
());
//boolean methodTypeMatched = (type instanceof Class)
// && responseType.isAssignableFrom((Class<?>) type);
if
(
methodTypeMatched
)
{
// 类型匹配:无需进一步解析,直接返回该对象
result
=
resp
;
}
else
{
// 类型不匹配:开始解析响应对象
this
.
ensureResponseSuccess
(
resp
);
result
=
this
.
parseResponse
(
resp
,
type
);
}
}
return
result
;
}
/**
* 处理非正常返回,即 2xx 之外的 HTTP 状态码
*
* @param response feign 请求
* @throws IOException 异常
* @deprecated 貌似捕捉不到 2xx 之外的响应
*/
@Deprecated
private
void
handleErrorResponse
(
Response
response
)
throws
IOException
{
Response
.
Body
body
=
response
.
body
();
if
(
body
==
null
)
{
throw
BusinessException
.
create
(
ResultEnum
.
REMOTE_SERVICE_ERROR
);
}
else
{
String
respJson
=
IoUtil
.
read
(
body
.
asReader
(
StandardCharsets
.
UTF_8
));
Type
responseType
=
this
.
getResponseType
();
T
resp
=
JSON
.
parseObject
(
respJson
,
responseType
);
throw
BusinessException
.
create
(
this
.
extractCode
(
resp
),
this
.
extractMessage
(
resp
));
}
}
///**
// * 获取响应对象类型
// *
// * @return 类型
// */
//@SuppressWarnings("unchecked")
//private Class<T> getResponseType() {
// ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
// return (Class<T>) type.getActualTypeArguments()[0];
//}
/**
* 获取响应对象类型
*
* @return 类型
*/
private
Type
getResponseType
()
{
ParameterizedType
type
=
(
ParameterizedType
)
this
.
getClass
().
getGenericSuperclass
();
return
type
.
getActualTypeArguments
()[
0
];
}
/**
* 解析响应对象中的内容
*
* @param resp 响应对象
* @param type 解析的目标类型
* @return 结果
*/
private
Object
parseResponse
(
T
resp
,
Type
type
)
{
Object
result
=
(
this
.
extractData
(
resp
)
==
null
)
?
null
:
this
.
getParser
(
type
).
parse
(
resp
,
type
);
return
result
;
}
/**
* 获取响应对象解析器
*
* @param type 解析的目标类型
* @return 解析器
*/
protected
abstract
FeignParser
<
T
>
getParser
(
Type
type
);
/**
* 提取响应对象中的状态码
*
* @param resp 响应对象
* @return 结果
*/
protected
abstract
int
extractCode
(
T
resp
);
/**
* 提取响应对象中的信息
*
* @param resp 响应对象
* @return 结果
*/
protected
abstract
String
extractMessage
(
T
resp
);
/**
* 提取响应对象中的数据
*
* @param resp 响应对象
* @return 结果
*/
protected
abstract
Object
extractData
(
T
resp
);
/**
* 确定响应对象的返回是正常的,非正常则抛出异常
*
* @param resp 响应对象
*/
protected
abstract
void
ensureResponseSuccess
(
T
resp
);
/**
* 响应对象解析类,解析成最终需要的方法出参对象
*
* @param <T> 方法出参类型
*/
protected
interface
FeignParser
<
T
>
{
/**
* 解析对象成方法返回对象
*
* @param resp 响应体
* @param type 方法出参类型
* @return 结果
*/
Object
parse
(
T
resp
,
Type
type
);
}
}
src/main/java/com/glsx/admin/config/FeignDecoderConfig.java
0 → 100644
View file @
c00509e
package
com
.
glsx
.
admin
.
config
;
import
feign.codec.Decoder
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.context.annotation.Bean
;
/**
* <p>Description: feign 解码器配置
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-05-08
*/
@Slf4j
//@Configuration
public
class
FeignDecoderConfig
{
@Bean
public
Decoder
decoderForR
()
{
return
new
FeignDecoderForR
();
}
}
src/main/java/com/glsx/admin/config/FeignDecoderForR.java
0 → 100644
View file @
c00509e
package
com
.
glsx
.
admin
.
config
;
import
java.lang.reflect.ParameterizedType
;
import
java.lang.reflect.Type
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.github.pagehelper.Page
;
import
com.github.pagehelper.PageInfo
;
import
com.glsx.admin.exception.BusinessException
;
import
com.glsx.admin.exception.ResultEnum
;
import
com.glsx.plat.core.web.R
;
/**
* <p>Description: {@link R} 的 feign 解码器
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-05-08
*/
public
class
FeignDecoderForR
extends
FeignDecoder
<
R
>
{
@Override
protected
FeignParser
<
R
>
getParser
(
Type
type
)
{
return
FeignParserForR
.
getParser
(
type
);
}
@Override
protected
int
extractCode
(
R
resp
)
{
return
resp
.
getCode
();
}
@Override
protected
String
extractMessage
(
R
resp
)
{
return
resp
.
getMessage
();
}
@Override
protected
Object
extractData
(
R
resp
)
{
return
resp
.
getData
();
}
@Override
protected
void
ensureResponseSuccess
(
R
resp
)
{
int
code
=
resp
.
getCode
();
String
message
=
resp
.
getMessage
();
if
(!
ResultEnum
.
SUCCESS
.
getCode
().
equals
(
code
))
{
throw
BusinessException
.
create
(
code
,
message
);
}
}
/**
* {@link R} 的响应对象解析器
*/
private
enum
FeignParserForR
implements
FeignParser
<
R
>
{
/**
* 直接按照 type 解析成对应类型的对象
*/
COMMON
(
Object
.
class
)
{
@Override
public
Object
parse
(
R
resp
,
Type
type
)
{
String
dataJson
=
JSON
.
toJSONString
(
resp
.
getData
());
return
JSON
.
parseObject
(
dataJson
,
type
);
}
},
/**
* 从 resp 中提取出 {@link PageInfo} 对象
*/
PAGE_HELPER_PAGE_INFO
(
PageInfo
.
class
)
{
@Override
public
Object
parse
(
R
resp
,
Type
type
)
{
String
dataJson
=
JSON
.
toJSONString
(
resp
.
getData
());
JSONObject
dataObj
=
JSON
.
parseObject
(
dataJson
);
// 先创造出 PageInfo 对象,再填充数据
PageInfo
<?>
pageInfo
=
PageInfo
.
of
(
dataObj
.
getJSONArray
(
"list"
));
pageInfo
.
setPages
(
dataObj
.
getInteger
(
"totalPages"
));
pageInfo
.
setTotal
(
dataObj
.
getLong
(
"totalElements"
));
return
pageInfo
;
}
},
/**
* 从 resp 中提取出 {@link Page} 对象
*/
PAGE_HELPER_PAGE
(
Page
.
class
)
{
@Override
public
Object
parse
(
R
resp
,
Type
type
)
{
ParameterizedType
parameterizedType
=
(
ParameterizedType
)
type
;
String
dataJson
=
JSON
.
toJSONString
(
resp
.
getData
());
JSONObject
dataObj
=
JSON
.
parseObject
(
dataJson
);
// 解析出 Page 对象,再填充数据
Page
<?>
page
=
dataObj
.
getJSONArray
(
"page"
).
toJavaObject
(
type
);
page
.
addAll
(
dataObj
.
getJSONArray
(
"items"
).
toJavaObject
(
parameterizedType
.
getActualTypeArguments
()[
0
]));
return
page
;
}
},
/**
* 从 resp 中提取出 {@link org.springframework.data.domain.Page} 对象
*/
SPRING_PAGE
(
org
.
springframework
.
data
.
domain
.
Page
.
class
)
{
@Override
public
Object
parse
(
R
resp
,
Type
type
)
{
String
dataJson
=
JSON
.
toJSONString
(
resp
.
getData
());
JSONObject
dataObj
=
JSON
.
parseObject
(
dataJson
);
// 直接解析吧,分页数据没办法填充
org
.
springframework
.
data
.
domain
.
Page
<?>
page
=
dataObj
.
getJSONArray
(
"list"
)
.
toJavaObject
(
type
);
return
page
;
}
},
;
private
final
Class
<?>
clazz
;
FeignParserForR
(
Class
<?>
clazz
)
{
this
.
clazz
=
clazz
;
}
public
static
FeignParser
<
R
>
getParser
(
Type
type
)
{
// 先赋值为默认解析器,防止下面的循环没有匹配到上面枚举中的任何一个解析器
FeignParser
<
R
>
parser
=
COMMON
;
// 判断是否是泛型出参
if
(
type
instanceof
ParameterizedType
)
{
// 是泛型出参:匹配上面枚举中的解析器
ParameterizedType
parameterizedType
=
(
ParameterizedType
)
type
;
Class
<?>
rawType
=
(
Class
<?>)
parameterizedType
.
getRawType
();
for
(
FeignParserForR
value
:
values
())
{
// 判断当前解析器支持类型是否和方法出参类型匹配
if
(
rawType
.
equals
(
value
.
clazz
))
{
parser
=
value
;
break
;
}
}
}
return
parser
;
}
}
}
src/main/java/com/glsx/admin/remote/IndexFeignClient.java
View file @
c00509e
package
com
.
glsx
.
admin
.
remote
;
import
com.glsx.admin.config.FeignDecoderConfig
;
import
com.glsx.admin.remote.impl.IndexHystrix
;
import
org.springframework.cloud.openfeign.FeignClient
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.ResponseBody
;
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
,
fallback
=
IndexHystrix
.
class
)
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
,
fallback
=
IndexHystrix
.
class
,
configuration
=
FeignDecoderConfig
.
class
)
public
interface
IndexFeignClient
{
@ResponseBody
...
...
src/main/java/com/glsx/admin/remote/OrderValueFeignClient.java
View file @
c00509e
package
com
.
glsx
.
admin
.
remote
;
import
java.util.List
;
import
com.glsx.admin.config.FeignDecoderConfig
;
import
com.glsx.admin.pojo.request.DeviceOrderValueReq
;
import
com.glsx.admin.pojo.response.DeviceOrderValueResp
;
import
org.springframework.cloud.openfeign.FeignClient
;
...
...
@@ -8,9 +10,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
java.util.List
;
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
)
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
,
configuration
=
FeignDecoderConfig
.
class
)
public
interface
OrderValueFeignClient
{
@ResponseBody
...
...
src/main/java/com/glsx/admin/remote/ProductConditionFeignClient.java
View file @
c00509e
package
com
.
glsx
.
admin
.
remote
;
import
java.util.List
;
import
com.glsx.admin.config.FeignDecoderConfig
;
import
com.glsx.admin.pojo.request.ScenceSupstatusReq
;
import
com.glsx.admin.pojo.response.ScenceSupstatusResp
;
import
com.glsx.admin.remote.impl.IndexHystrix
;
import
org.springframework.cloud.openfeign.FeignClient
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
java.util.List
;
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
)
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
,
configuration
=
FeignDecoderConfig
.
class
)
public
interface
ProductConditionFeignClient
{
@ResponseBody
...
...
src/main/java/com/glsx/admin/remote/ProductContentFeignClient.java
View file @
c00509e
package
com
.
glsx
.
admin
.
remote
;
import
java.util.List
;
import
com.glsx.admin.config.FeignDecoderConfig
;
import
com.glsx.admin.pojo.request.DeviceOrderReq
;
import
com.glsx.admin.pojo.response.DeviceOrderResp
;
import
org.springframework.cloud.openfeign.FeignClient
;
...
...
@@ -8,9 +10,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
java.util.List
;
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
)
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
,
configuration
=
FeignDecoderConfig
.
class
)
public
interface
ProductContentFeignClient
{
@ResponseBody
...
...
src/main/java/com/glsx/admin/remote/ProductFeignClient.java
View file @
c00509e
...
...
@@ -2,6 +2,7 @@ package com.glsx.admin.remote;
import
java.util.List
;
import
com.github.pagehelper.PageInfo
;
import
com.glsx.admin.config.FeignDecoderConfig
;
import
com.glsx.admin.pojo.request.ProductReq
;
import
com.glsx.admin.pojo.response.DeviceOrderResp
;
import
com.glsx.admin.pojo.response.ProductResp
;
...
...
@@ -19,7 +20,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
* @author wbh
* @date 2021-03-16
*/
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
)
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
,
configuration
=
FeignDecoderConfig
.
class
)
public
interface
ProductFeignClient
{
@RequestMapping
(
value
=
"/product/getPageList"
,
method
=
{
RequestMethod
.
GET
})
@ResponseBody
...
...
src/main/java/com/glsx/admin/remote/ProductTypeFeignClient.java
View file @
c00509e
...
...
@@ -2,6 +2,7 @@ package com.glsx.admin.remote;
import
java.util.List
;
import
com.github.pagehelper.PageInfo
;
import
com.glsx.admin.config.FeignDecoderConfig
;
import
com.glsx.admin.pojo.request.ProductTypeReq
;
import
com.glsx.admin.pojo.response.ProductTypeResp
;
import
org.springframework.cloud.openfeign.FeignClient
;
...
...
@@ -18,7 +19,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
* @author wbh
* @date 2021-03-17
*/
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
)
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
,
configuration
=
FeignDecoderConfig
.
class
)
public
interface
ProductTypeFeignClient
{
@RequestMapping
(
value
=
"/productType/getPageList"
,
method
=
{
RequestMethod
.
GET
})
@ResponseBody
...
...
src/main/java/com/glsx/admin/remote/SceneTemplateFeignClient.java
View file @
c00509e
package
com
.
glsx
.
admin
.
remote
;
import
java.util.List
;
import
com.github.pagehelper.PageInfo
;
import
com.glsx.admin.config.FeignDecoderConfig
;
import
com.glsx.admin.pojo.request.SceneTemplateReq
;
import
com.glsx.admin.pojo.response.SceneTemplateResp
;
import
org.springframework.cloud.openfeign.FeignClient
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.List
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.ResponseBody
;
/**
* <p>Description:
...
...
@@ -15,7 +19,8 @@ import java.util.List;
* @author think.wang
* @date 2021-03-16
*/
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
)
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
,
configuration
=
FeignDecoderConfig
.
class
)
public
interface
SceneTemplateFeignClient
{
@ResponseBody
...
...
src/main/java/com/glsx/admin/remote/SysCodeFeignClient.java
View file @
c00509e
package
com
.
glsx
.
admin
.
remote
;
import
java.util.List
;
import
com.glsx.admin.config.FeignDecoderConfig
;
import
com.glsx.admin.pojo.request.SysCodeReq
;
import
com.glsx.admin.pojo.response.SysCodeResp
;
import
org.springframework.cloud.openfeign.FeignClient
;
...
...
@@ -16,7 +17,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
* @author think.wang
* @date 2021-03-16
*/
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
)
@FeignClient
(
name
=
"${feign.server.name}"
,
url
=
"${feign.server.url}"
,
configuration
=
FeignDecoderConfig
.
class
)
public
interface
SysCodeFeignClient
{
@ResponseBody
@RequestMapping
(
value
=
"/sysCode/getListByCondition"
,
method
=
{
RequestMethod
.
POST
})
...
...
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