Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yd-oss
Overview
Overview
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
xingmin
yd-oss
Commits
9a940a61
Commit
9a940a61
authored
Oct 13, 2025
by
zhangxingmin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
push
parent
7ca1f888
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
8 deletions
+15
-8
yd-oss-service/src/main/java/com/yd/oss/service/service/impl/AnnotationFieldValueProvider.java
+15
-8
No files found.
yd-oss-service/src/main/java/com/yd/oss/service/service/impl/AnnotationFieldValueProvider.java
View file @
9a940a61
...
...
@@ -3,22 +3,29 @@ package com.yd.oss.service.service.impl;
import
com.yd.oss.feign.annotation.PdfField
;
import
com.yd.oss.service.service.FieldValueProvider
;
import
org.springframework.stereotype.Component
;
import
java.lang.reflect.Field
;
import
java.util.Map
;
/**
* 使用注解实现通用字段值提供器
* @param <T>
* 支持注解和 Map 的通用字段值提供器
*/
@Component
public
class
AnnotationFieldValueProvider
<
T
>
implements
FieldValueProvider
<
T
>
{
@Override
@SuppressWarnings
(
"unchecked"
)
public
String
getFieldValue
(
T
object
,
String
fieldName
)
{
try
{
// 获取所有字段
// 如果对象是 Map 类型
if
(
object
instanceof
Map
)
{
Map
<
String
,
Object
>
map
=
(
Map
<
String
,
Object
>)
object
;
Object
value
=
map
.
get
(
fieldName
);
return
value
!=
null
?
value
.
toString
()
:
""
;
}
// 如果是普通 Java 对象,使用注解方式
Field
[]
fields
=
object
.
getClass
().
getDeclaredFields
();
for
(
Field
field
:
fields
)
{
PdfField
annotation
=
field
.
getAnnotation
(
PdfField
.
class
);
if
(
annotation
!=
null
&&
annotation
.
value
().
equals
(
fieldName
))
{
...
...
@@ -27,7 +34,7 @@ public class AnnotationFieldValueProvider<T> implements FieldValueProvider<T> {
return
value
!=
null
?
value
.
toString
()
:
""
;
}
}
// 如果没有找到注解,尝试使用字段名
try
{
Field
field
=
object
.
getClass
().
getDeclaredField
(
fieldName
);
...
...
@@ -37,7 +44,7 @@ public class AnnotationFieldValueProvider<T> implements FieldValueProvider<T> {
}
catch
(
NoSuchFieldException
e
)
{
return
""
;
}
}
catch
(
Exception
e
)
{
return
""
;
}
...
...
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