jar

一、JsonArrayName

import java.lang.annotation.*;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface JsonArrayName {
public String name();
}
二、JsonFieldName
import java.lang.annotation.*;
/**
* 普通字段注解
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface JsonFieldName {
public String name();
}
三、JsonObjectName
import java.lang.annotation.*;
/**
* Object字段注解
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface JsonObjectName {
public String name();
}
四、JsonNameBinding
import com.alibaba.fastjson.JSON;
import com.xxx.annotations.JsonArrayName;
import com.xxx.annotations.JsonFieldName;
import com.xxx.annotations.JsonObjectName;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;


/**
* 标签处理器
* 默认对象的成员变量所处的先后顺序进行排序
* 默认没有注解的类将按字段名命名(集合类型中的形参的字段名亦不解析)
*/
public class JsonNameBinding {

public static String getJson(Object object) throws Exception {
return JSON.toJSONString(JsonNameBinding.convert(object));
}

private static LinkedHashMap<Object, Object> convert(Object object) throws IllegalAccessException {
LinkedHashMap<Object, Object> linkedHashMap = new LinkedHashMap<Object, Object>();
Class objectClass = object.getClass();
Field[] fields = objectClass.getDeclaredFields();
for (Field field : fields) {
JsonArrayName jsonListName = field.getAnnotation(JsonArrayName.class);
JsonFieldName jsonFieldName = field.getAnnotation(JsonFieldName.class);
JsonObjectName jsonObjectName = field.getAnnotation(JsonObjectName.class);
Class type = field.getType();
field.setAccessible(true);
Object value = field.get(object);
if (value != null) {
if (jsonListName != null) {
List list = new ArrayList();
List list1 = (List) value;
for (Object o : list1) {
list.add(JsonNameBinding.convert(o));
}
linkedHashMap.put(jsonListName.name(), list);
} else if (jsonFieldName != null) {
boolean f=(value!=null && !StringUtils.isBlank(value.toString()));
if(value!=null && !StringUtils.isBlank(value.toString())){
linkedHashMap.put(jsonFieldName.name(), value);
}

// if (!"".equals(String.valueOf(value))) {
// linkedHashMap.put(jsonFieldName.name(), value);
// }
} else if (jsonObjectName != null) {
linkedHashMap.put(jsonObjectName.name(), JsonNameBinding.convert(value));
}
}
}
return linkedHashMap;
}

}
五、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.xxx.annotation</groupId>
<artifactId>annotation</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>

<build>
<finalName>annotation</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- 指定在打包节点执行jar包合并操作 -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
posted @ 2019-02-20 10:08  albert_think  阅读(477)  评论(0编辑  收藏  举报