您的位置:首页 > 新闻 > 资讯 > 在线观看免费网站网址_企点官网下载安装_站内搜索工具_上海网站建设优化

在线观看免费网站网址_企点官网下载安装_站内搜索工具_上海网站建设优化

2024/12/27 15:42:35 来源:https://blog.csdn.net/lianghyan/article/details/144117279  浏览:    关键词:在线观看免费网站网址_企点官网下载安装_站内搜索工具_上海网站建设优化
在线观看免费网站网址_企点官网下载安装_站内搜索工具_上海网站建设优化

https://www.baeldung.com/java-custom-annotation

阅读了baeldung里的例子,把code抽取出来,作为参考

package anno;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

public @interface Init {

}

package anno;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.FIELD)

public @interface JsonElement {

public String key() default "";

}

package anno;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

public @interface JsonSerializable {

}

package anno;

@JsonSerializable

public class Person {

@JsonElement

private String firstName;

@JsonElement

private String lastName;

@JsonElement(key = "personAge")

private String age;

private String address;

public Person(String firstName, String lastName, String age) {

this.firstName = firstName;

this.lastName=lastName;

this.age=age;

}

@Init

private void initNames() {

this.firstName = this.firstName.substring(0, 1).toUpperCase()

+ this.firstName.substring(1);

this.lastName = this.lastName.substring(0, 1).toUpperCase()

+ this.lastName.substring(1);

}

// Standard getters and setters

}

package anno;

public class JsonSerializationException extends Exception {

public JsonSerializationException(String message) {

super(message);

}

}

package anno;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import java.util.HashMap;

import java.util.Map;

import java.util.Objects;

import java.util.stream.Collectors;

public class ObjectToJsonConverter {

public String convertToJson(Object object) throws JsonSerializationException {

try {

checkIfSerializable(object);

initializeObject(object);

return getJsonString(object);

} catch (Exception e) {

throw new JsonSerializationException(e.getMessage());

}

}

private void checkIfSerializable(Object object) throws JsonSerializationException {

if (Objects.isNull(object)) {

throw new JsonSerializationException("The object to serialize is null");

}

Class<?> clazz = object.getClass();

if (!clazz.isAnnotationPresent(JsonSerializable.class)) {

throw new JsonSerializationException("The class "

+ clazz.getSimpleName()

+ " is not annotated with JsonSerializable");

}

}

private void initializeObject(Object object) throws Exception {

Class<?> clazz = object.getClass();

for (Method method : clazz.getDeclaredMethods()) {

if (method.isAnnotationPresent(Init.class)) {

method.setAccessible(true);

method.invoke(object);

}

}

}

private String getJsonString(Object object) throws Exception {

Class<?> clazz = object.getClass();

Map<String, String> jsonElementsMap = new HashMap<>();

for (Field field : clazz.getDeclaredFields()) {

field.setAccessible(true);

if (field.isAnnotationPresent(JsonElement.class)) {

jsonElementsMap.put(getKey(field), (String) field.get(object));

}

}

String jsonString = jsonElementsMap.entrySet()

.stream()

.map(entry -> "\"" + entry.getKey() + "\":\""

+ entry.getValue() + "\"")

.collect(Collectors.joining(","));

return "{" + jsonString + "}";

}

private String getKey(Field field) {

JsonElement je= field.getAnnotation(JsonElement.class);

if(je.key()!=null && je.key().length()>0) {

return je.key();

}else {

return field.getName();

}

}

}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com