Jackson 对象与json数据互转工具类JacksonUtil
下面是一个基于 Jackson 的工具类 JacksonUtil,用于在 Java 项目中实现对象与 JSON 数据之间的互相转换。该工具类具有简洁、易用、通用的特点。
package com.fy.common.util;import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.fy.common.constant.ObjectConstant;
import com.fy.common.constant.StringConstant;
import com.fy.common.custom.CustomDateDeserializer;
import com.fy.common.custom.CustomDateSerializer;
import lombok.NonNull;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;import java.io.*;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public enum JacksonUtil {/*** 单例*/i;/*** convert json to javabean or javabean to json.*/private ObjectMapper mapper;private TypeReference<Map<String, Object>> mapType = new TypeReference<Map<String, Object>>() {} ;private TypeReference<List<Map<String, Object>>> listmType = new TypeReference<List<Map<String, Object>>>() {};static {i.mapper = new ObjectMapper();i.mapper.setTimeZone(ObjectConstant.DatexFormatter.TIME_ZONE_E8);// 对于空的对象转json的时候不抛出错误i.mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);// 禁用遇到未知属性抛出异常i.mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);// 序列化BigDecimal时不使用科学计数法输出i.mapper.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);i.mapper.registerModule(sqltimeModule());i.mapper.registerModule(localtimeModule());}private static SimpleModule sqltimeModule() {SimpleModule sqltimeModule = new SimpleModule();sqltimeModule.addSerializer(Timestamp.class, new CustomDateSerializer.TimestampSerializer());sqltimeModule.addSerializer(Date.class, new CustomDateSerializer.SqlDateSerializer());sqltimeModule.addSerializer(Time.class, new CustomDateSerializer.SqlTimeSerializer());sqltimeModule.addDeserializer(Timestamp.class, new CustomDateDeserializer.TimestampDeserializer());sqltimeModule.addDeserializer(Date.class, new CustomDateDeserializer.SqlDateDeserializer());sqltimeModule.addDeserializer(Time.class, new CustomDateDeserializer.SqlTimeDeserializer());return sqltimeModule;}private static JavaTimeModule localtimeModule() {JavaTimeModule localtimeModule = new JavaTimeModule();LocalDateTimeSerializer datimeSlizer = new LocalDateTimeSerializer(ObjectConstant.DatexFormatter.STANDARD_DATETIME);LocalDateSerializer dateSlizer = new LocalDateSerializer(ObjectConstant.DatexFormatter.STANDARD_DATE);LocalTimeSerializer timeSlizer = new LocalTimeSerializer(ObjectConstant.DatexFormatter.STANDARD_TIME);LocalDateTimeDeserializer datimeDlizer = new LocalDateTimeDeserializer(ObjectConstant.DatexFormatter.STANDARD_DATETIME);LocalDateDeserializer dateDlizer = new LocalDateDeserializer(ObjectConstant.DatexFormatter.STANDARD_DATE);LocalTimeDeserializer timeDlizer = new LocalTimeDeserializer(ObjectConstant.DatexFormatter.STANDARD_TIME);localtimeModule.addSerializer(LocalDateTime.class, datimeSlizer);localtimeModule.addSerializer(LocalDate.class, dateSlizer);localtimeModule.addSerializer(LocalTime.class, timeSlizer);localtimeModule.addDeserializer(LocalDateTime.class, datimeDlizer);localtimeModule.addDeserializer(LocalDate.class, dateDlizer);localtimeModule.addDeserializer(LocalTime.class, timeDlizer);return localtimeModule;}/*** 创建一个JSON对象*/public static ObjectNode createObjectNode() {return i.mapper.createObjectNode();}/*** 创建一个JSON数组*/public static ArrayNode createArrayNode() {return i.mapper.createArrayNode();}/*** 判断一个字符串是否是合法的json格式字符串**/public static boolean isJson(String jsonStr) {try {i.mapper.readTree(jsonStr);return true;} catch (JsonProcessingException e) {return false;}}/*** 字符串转Json对象**/public static ObjectNode readJsonObject(String jsonStr) {JsonNode node = null;ObjectNode objNode = null;try {node = i.mapper.readTree(jsonStr);if (!node.isArray()) {objNode = (ObjectNode) node;}} catch (IOException e) {throw new RuntimeException(e);}return objNode;}/*** 字符串转Json数组*/public static ArrayNode readJsonArray(String jsonStr) {JsonNode node = null;ArrayNode arrNode = null;try {node = i.mapper.readTree(jsonStr);if (node.isArray()) {arrNode = (ArrayNode) node;}} catch (IOException e) {throw new RuntimeException(e);}return arrNode;}/*** 字符串转JsonNode*/public static JsonNode readJson(String jsonStr) {try {return i.mapper.readTree(jsonStr);} catch (IOException e) {throw new RuntimeException(e);}}/*** 字节数组转JSON*/public static JsonNode readJson(byte[] content) {try {return i.mapper.readTree(content);} catch (IOException e) {throw new RuntimeException(e);}}/*** 将JSON对象转成Map** @param objectNode json非数组对象* @return Map*/public static Map<String, Object> jsonObjct2Map(ObjectNode objectNode) {return i.mapper.convertValue(objectNode, i.mapType);}/*** 将JSON对象转成类型** @param objectNode json非数组对象* @return Map*/public static <T> T jsonObjct2Object(ObjectNode objectNode, Class<T> toType) {return i.mapper.convertValue(objectNode, toType);}/*** 将JSON对象转成List<Map>集合** @param arryNode json数组对象* @return List*/public static List<Map<String, Object>> jsonArray2List(ArrayNode arryNode) {return i.mapper.convertValue(arryNode, i.listmType);}/*** 将JsonNode转成指定类型的** @param jsonNode* @return T*/public static <T> T jsonNode2Type(JsonNode jsonNode, TypeReference<T> typeReference) {return i.mapper.convertValue(jsonNode, typeReference);}/*** 将字符串转成指定class类型对象*/public static <T> T readJson(String jsonStr, Class<T> T) {T obj = null;try {obj = i.mapper.readValue(jsonStr, T);} catch (JsonProcessingException e) {throw new RuntimeException(e);}return obj;}/*** 将字符串转成指定类型的对象*/public static <T> T readJson(String jsonStr, TypeReference<T> typeReference) {T obj = null;try {obj = i.mapper.readValue(jsonStr, typeReference);} catch (JsonProcessingException e) {throw new RuntimeException(e);}return obj;}/*** URL请求返回结果体转换对象* 案例: URL url = new URL("<a href="https://jsonplaceholder.typicode.com/posts/1">...</a>"); //远程服务URL* PostDTO post = JacksonUtil.readJson(url, PostDTO.class);*/public static <T> T readJson(@NonNull URL url, Class<T> type) {try {return i.mapper.readValue(url, type);} catch (IOException e) {throw new RuntimeException(e);}}/*** URL请求返回结果体转换对象*/public static <T> T readJson(@NonNull URL url, TypeReference<T> type) {try {return i.mapper.readValue(url, type);} catch (IOException e) {throw new RuntimeException(e);}}/*** URL请求返回结果体转换list集合*/public static <T> List<T> readJsonList(@NonNull URL url, Class<T> type) {try {CollectionType collectionType = i.mapper.getTypeFactory().constructCollectionType(ArrayList.class, type);return i.mapper.readValue(url, collectionType);} catch (IOException e) {throw new RuntimeException(e);}}/*** 输入流转换为对象*/public static <T> T readJson(@NonNull InputStream inputStream, Class<T> type) {try {return i.mapper.readValue(inputStream, type);} catch (IOException e) {throw new RuntimeException(e);}}/*** 输入流转换为对象*/public static <T> T readJson(@NonNull InputStream inputStream, TypeReference<T> type) {try {return i.mapper.readValue(inputStream, type);} catch (IOException e) {throw new RuntimeException(e);}}/*** 读取文件中JSON格式文本转换对象*/public static <T> T readJson(@NonNull File file, Class<T> type) {try {return i.mapper.readValue(file, type);} catch (IOException e) {throw new RuntimeException(e);}}/*** 读取文件中JSON格式文本转换对象*/public static <T> T readJson(@NonNull File file, TypeReference<T> type) {try {return i.mapper.readValue(file, type);} catch (IOException e) {throw new RuntimeException(e);}}/*** 读取文件中JSON格式文本转换(List)对象*/public static <T> List<T> readJsonList(@NonNull File file, Class<T> type) {try {CollectionType collectionType = i.mapper.getTypeFactory().constructCollectionType(ArrayList.class, type);return i.mapper.readValue(file, collectionType);} catch (IOException e) {throw new RuntimeException(e);}}/*** json字符串转换List集合* @param jsonString JSON字符串* @param cls 转换对象* @return list集合* @param <T> 目标对象类型*/public static <T> List<T> readJsonList(@NonNull String jsonString, Class<T> cls) {try {return i.mapper.readValue(jsonString, getCollectionType(cls));} catch (IOException e) {throw new RuntimeException(e);}}/*** Object对象转List集合*/public static <T> List<T> readJsonList(@NonNull Object obj, Class<T> cls) {try {String s = i.mapper.writeValueAsString(obj);return readJsonList(s, cls);} catch (JsonProcessingException e) {throw new RuntimeException(e);}}/*** JSON字符串转换Map集合*/public static Map<String, Object> readJsonMap(@NonNull String json) {if (StringUtils.isEmpty(json)) {return null;}try {MapType mapType = i.mapper.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class);return i.mapper.readValue(json, mapType);} catch (IOException e) {throw new RuntimeException(e);}}/*** 将java对象转成字符串*/public static String writeJson(Object entity) {String str = StringConstant.Json.EMPTY_OBJECT;if (entity == null) {return str;}try {str = i.mapper.writeValueAsString(entity);} catch (JsonProcessingException e) {throw new RuntimeException(e);}return str;}/*** list集合转换为JSON字符串* @param list list集合* @return JSON字符串* @param <T> 目标对象类型*/public static <T> String writeString(@NonNull List<T> list) {try {return i.mapper.writeValueAsString(list);} catch (JsonProcessingException e) {throw new RuntimeException(e);}}/*** 序列化为JSON*/public static <T> void writeFile(@NonNull String path, List<T> list) {try (Writer writer = new FileWriter(path, true)) {i.mapper.writer().writeValues(writer).writeAll(list);} catch (Exception e) {throw new RuntimeException(e);}}/*** 序列化为JSON*/public static <T> void writeFile(@NonNull String path, T t) {try (Writer writer = new FileWriter(path, true)) {i.mapper.writer().writeValues(writer).write(t);} catch (Exception e) {throw new RuntimeException(e);}}/*** 将对象实体转换成Jackson的ObjectNode*/public static ObjectNode bean2ObjNode(Object bean) {return i.mapper.convertValue(bean, ObjectNode.class);}/*** 将对象转换成JsonNode*/public static JsonNode convertToJsonNode (Object object) {return i.mapper.valueToTree(object);}/*** 将Map转换成JsonNode*/public static JsonNode convertToJsonNode(Map<String,Object> map) {return i.mapper.convertValue(map, JsonNode.class);}/*** 从json串中获取某个字段** @param json JSON字符串* @param key 对象属性名* @return String,默认为 null*/public static String getAsString(String json, String key) {if (StringUtils.isEmpty(json)) {return null;}try {JsonNode jsonNode = getAsJsonObject(json, key);if (null == jsonNode) {return null;}return getAsString(jsonNode);} catch (Exception e) {throw new RuntimeException(e);}}private static String getAsString(JsonNode jsonNode) {return jsonNode.isTextual() ? jsonNode.textValue() : jsonNode.toString();}/*** 从json串中获取某个字段** @param json JSON字符串* @param key 对象属性名* @return int,默认为 0*/public static int getAsInt(String json, String key) {if (StringUtils.isEmpty(json)) {return 0;}try {JsonNode jsonNode = getAsJsonObject(json, key);if (null == jsonNode) {return 0;}return jsonNode.isInt() ? jsonNode.intValue() : Integer.parseInt(getAsString(jsonNode));} catch (Exception e) {throw new RuntimeException(e);}}/*** 从json串中获取某个字段** @param json JSON字符串* @param key 对象属性名* @return long,默认为 0*/public static long getAsLong(String json, String key) {if (StringUtils.isEmpty(json)) {return 0L;}try {JsonNode jsonNode = getAsJsonObject(json, key);if (null == jsonNode) {return 0L;}return jsonNode.isLong() ? jsonNode.longValue() : Long.parseLong(getAsString(jsonNode));} catch (Exception e) {throw new RuntimeException(e);}}/*** 从json串中获取某个字段** @param json JSON字符串* @param key 对象属性名* @return double,默认为 0.0*/public static double getAsDouble(String json, String key) {if (StringUtils.isEmpty(json)) {return 0.0;}try {JsonNode jsonNode = getAsJsonObject(json, key);if (null == jsonNode) {return 0.0;}return jsonNode.isDouble() ? jsonNode.doubleValue() : Double.parseDouble(getAsString(jsonNode));} catch (Exception e) {throw new RuntimeException(e);}}/*** 从json串中获取某个字段** @param json JSON字符串* @param key 对象属性名* @return BigInteger,默认为 0.0*/public static BigInteger getAsBigInteger(String json, String key) {if (StringUtils.isEmpty(json)) {return new BigInteger(String.valueOf(0.00));}try {JsonNode jsonNode = getAsJsonObject(json, key);if (null == jsonNode) {return new BigInteger(String.valueOf(0.00));}return jsonNode.isBigInteger() ? jsonNode.bigIntegerValue() : new BigInteger(getAsString(jsonNode));} catch (Exception e) {throw new RuntimeException(e);}}/*** 从json串中获取某个字段** @param json JSON字符串* @param key 对象属性名* @return BigDecimal,默认为 0.00*/public static BigDecimal getAsBigDecimal(String json, String key) {if (StringUtils.isEmpty(json)) {return new BigDecimal("0.00");}try {JsonNode jsonNode = getAsJsonObject(json, key);if (null == jsonNode) {return new BigDecimal("0.00");}return jsonNode.isBigDecimal() ? jsonNode.decimalValue() : new BigDecimal(getAsString(jsonNode));} catch (Exception e) {throw new RuntimeException(e);}}/*** 从json串中获取某个字段** @param json JSON字符串* @param key 对象属性名* @return boolean, 默认为false*/public static boolean getAsBoolean(String json, String key) {if (StringUtils.isEmpty(json)) {return false;}try {JsonNode jsonNode = getAsJsonObject(json, key);if (null == jsonNode) {return false;}if (jsonNode.isBoolean()) {return jsonNode.booleanValue();} else {if (jsonNode.isTextual()) {String textValue = jsonNode.textValue();if (StringConstant.Number.ONE.equals(textValue)) {return true;} else {return BooleanUtils.toBoolean(textValue);}} else {//numberreturn BooleanUtils.toBoolean(jsonNode.intValue());}}} catch (Exception e) {throw new RuntimeException(e);}}/*** 从json串中获取某个字段** @param json JSON字符串* @param key 对象属性名* @return byte[], 默认为 null*/public static byte[] getAsBytes(String json, String key) {if (StringUtils.isEmpty(json)) {return null;}try {JsonNode jsonNode = getAsJsonObject(json, key);if (null == jsonNode) {return null;}return jsonNode.isBinary() ? jsonNode.binaryValue() : getAsString(jsonNode).getBytes();} catch (Exception e) {throw new RuntimeException(e);}}/*** 从json串中获取某个字段** @param json JSON字符串* @param key 对象属性名* @return object, 默认为 null*/public static <T> T getAsObject(String json, String key, Class<T> type) {if (StringUtils.isEmpty(json)) {return null;}try {JsonNode jsonNode = getAsJsonObject(json, key);if (null == jsonNode) {return null;}JavaType javaType = i.mapper.getTypeFactory().constructType(type);return from(getAsString(jsonNode), javaType);} catch (Exception e) {throw new RuntimeException(e);}}/*** 从json串中获取某个字段** @param json JSON字符串* @param key 对象属性名* @return list, 默认为 null*/public static <T> List<T> getAsList(String json, String key, Class<T> type) {if (StringUtils.isEmpty(json)) {return null;}try {JsonNode jsonNode = getAsJsonObject(json, key);if (null == jsonNode) {return null;}CollectionType collectionType = i.mapper.getTypeFactory().constructCollectionType(ArrayList.class, type);return from(getAsString(jsonNode), collectionType);} catch (Exception e) {throw new RuntimeException(e);}}/*** JSON反序列化*/private static <T> T from(String json, Type type) {if (StringUtils.isEmpty(json)) {return null;}try {JavaType javaType = i.mapper.getTypeFactory().constructType(type);return i.mapper.readValue(json, javaType);} catch (IOException e) {throw new RuntimeException(e);}}/*** 从json串中获取某个字段* @return JsonNode, 默认为 null*/public static JsonNode getAsJsonObject(String json, String key) {try {JsonNode node = i.mapper.readTree(json);if (null == node) {return null;}return node.get(key);} catch (IOException e) {throw new RuntimeException(e);}}/*** 向json中添加属性* @return json*/public static <T> String add(String json, String key, T value) {try {JsonNode node = i.mapper.readTree(json);add(node, key, value);return node.toString();} catch (IOException e) {throw new RuntimeException(e);}}/*** 向json中添加属性*/private static <T> void add(JsonNode jsonNode, String key, T value) {if (value instanceof String) {((ObjectNode) jsonNode).put(key, (String) value);} else if (value instanceof Short) {((ObjectNode) jsonNode).put(key, (Short) value);} else if (value instanceof Integer) {((ObjectNode) jsonNode).put(key, (Integer) value);} else if (value instanceof Long) {((ObjectNode) jsonNode).put(key, (Long) value);} else if (value instanceof Float) {((ObjectNode) jsonNode).put(key, (Float) value);} else if (value instanceof Double) {((ObjectNode) jsonNode).put(key, (Double) value);} else if (value instanceof BigDecimal) {((ObjectNode) jsonNode).put(key, (BigDecimal) value);} else if (value instanceof BigInteger) {((ObjectNode) jsonNode).put(key, (BigInteger) value);} else if (value instanceof Boolean) {((ObjectNode) jsonNode).put(key, (Boolean) value);} else if (value instanceof byte[]) {((ObjectNode) jsonNode).put(key, (byte[]) value);} else {((ObjectNode) jsonNode).put(key, writeJson(value));}}/*** 除去json中的某个属性* @return json*/public static String remove(String json, String key) {try {JsonNode node = i.mapper.readTree(json);((ObjectNode) node).remove(key);return node.toString();} catch (IOException e) {throw new RuntimeException(e);}}/*** 修改json中的属性*/public static <T> String update(String json, String key, T value) {try {JsonNode node = i.mapper.readTree(json);((ObjectNode) node).remove(key);add(node, key, value);return node.toString();} catch (IOException e) {throw new RuntimeException(e);}}/*** 格式化Json(美化)* @return json*/public static String format(String json) {try {JsonNode node = i.mapper.readTree(json);return i.mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);} catch (IOException e) {throw new RuntimeException(e);}}/*** 获取泛型的Collection Type** @param elementClasses 实体bean* @return JavaType Java类型*/private static JavaType getCollectionType(Class<?>... elementClasses) {return i.mapper.getTypeFactory().constructParametricType(List.class, elementClasses);}}
package com.fy.common.custom;import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.SneakyThrows;import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.io.IOException;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.GregorianCalendar;/*** 全局日期(针对 java.sql.Date、java.sql.Time、java.sql.TimeStamp三种)* jackson反解析*/
public class CustomDateDeserializer {public static class TimestampDeserializer extends JsonDeserializer<Timestamp> {@Overridepublic Timestamp deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {return Timestamp.valueOf(p.getValueAsString());}}public static class SqlDateDeserializer extends JsonDeserializer<Date> {@Overridepublic Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {return Date.valueOf(p.getValueAsString());}}public static class SqlTimeDeserializer extends JsonDeserializer<Time> {@Overridepublic Time deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {return Time.valueOf(p.getValueAsString());}}public static class SqlXMLGregorianCalendarDeserializer extends JsonDeserializer<XMLGregorianCalendar> {@SneakyThrows@Overridepublic XMLGregorianCalendar deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {Date date = Date.valueOf(p.getValueAsString());GregorianCalendar cal = new GregorianCalendar();cal.setTime(date);return DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);}}}
package com.fy.common.custom;import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;import javax.xml.datatype.XMLGregorianCalendar;
import java.io.IOException;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;/*** 全局日期(针对 java.sql.Date、java.sql.Time、java.sql.TimeStamp三种)* jackson解析*/
public class CustomDateSerializer {public static class SqlDateSerializer extends JsonSerializer<Date> {@Overridepublic void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException {gen.writeString(value.toString());}}public static class TimestampSerializer extends JsonSerializer<Timestamp> {@Overridepublic void serialize(Timestamp value, JsonGenerator gen, SerializerProvider serializers) throws IOException {gen.writeString(value.toString().substring(0, 19));}}public static class SqlTimeSerializer extends JsonSerializer<Time> {@Overridepublic void serialize(Time value, JsonGenerator gen, SerializerProvider serializers) throws IOException {gen.writeString(value.toString());}}public static class SqlXMLGregorianCalendarSerializer extends JsonSerializer<XMLGregorianCalendar> {@Overridepublic void serialize(XMLGregorianCalendar value, JsonGenerator gen, SerializerProvider serializers) throws IOException {gen.writeNumber(value.toGregorianCalendar().getTimeInMillis());}}}
其中StringConstant.JSON在这篇博客中有分享。
https://blog.csdn.net/qq_41520636/article/details/144006665?spm=1001.2014.3001.5501
相关文章:
Jackson 对象与json数据互转工具类JacksonUtil
下面是一个基于 Jackson 的工具类 JacksonUtil,用于在 Java 项目中实现对象与 JSON 数据之间的互相转换。该工具类具有简洁、易用、通用的特点。 package com.fy.common.util;import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core…...
【ArcGISPro】根据yaml构建原始Pro的conda环境
使用场景 我们不小心把原始arcgispro-py3的conda环境破坏了,我们就可以使用以下方法进行修复 查找文件 在arcgis目录下找到yaml文件 如果没找到请复制以下内容到新的yaml文件 channels: - esri - defaults dependencies: - anyio=4.2.0=py311haa95532_0 - appdirs=1.4.4=p…...
对撞双指针(七)三数之和
15. 三数之和 给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i ! j、i ! k 且 j ! k ,同时还满足 nums[i] nums[j] nums[k] 0 。请你返回所有和为 0 且不重复的三元组。 注意:答案中不可以包含重复的三元组…...
反向代理服务器的用途
代理服务器在网络中扮演着重要的角色,它们可以优化流量、保护服务器以及提高安全性。在代理服务器中,反向代理服务器是一种特殊类型,常用于Web服务器前,它具备多种功能,能够确保网络流量的顺畅传输。那么,让…...
一些好的AI技术学习平台和资料(动态更新)
1. 大模型 1.1 提示词(Prompt) 目前,大模型技术已经深入到工作生活的方方面面,各技术大厂的大模型也层出不穷,从开始的OpenAI一家独大,到当今世界的“百模大战”。从一些日常使用的角度来说,模…...
wireshark抓包分析HTTP协议,HTTP协议执行流程,
「作者主页」:士别三日wyx 「作者简介」:CSDN top100、阿里云博客专家、华为云享专家、网络安全领域优质创作者 「推荐专栏」:对网络安全感兴趣的小伙伴可以关注专栏《网络安全入门到精通》 使用WireShark工具抓取「HTTP协议」的数据包&#…...
路由缓存后跳转到新路由时,上一路由中的tip信息框不销毁问题解决
上一路由tip信息框不销毁问题解决 路由缓存篇问题描述及截图解决思路关键代码 路由缓存篇 传送门 问题描述及截图 路由缓存后跳转新路由时,上一个路由的tip信息框没销毁。 解决思路 在全局路由守卫中获取DOM元素,通过css去控制 关键代码 修改文…...
【Angular】async详解
在 Angular 中,async 关键字用于定义异步函数,通常与 await 一起使用来处理 Promise。这使得异步代码看起来更像同步代码,从而更容易理解和维护。 基本用法 定义异步函数:使用 async 关键字。等待 Promise 解析:使用…...
springboot/ssm综合小区管理系统Java社区物业停车缴费系统web物业源码
springboot/ssm综合小区管理系统Java社区物业停车缴费系统web物业源码 基于springboot(可改ssm)htmlvue项目 开发语言:Java 框架:springboot/可改ssm vue JDK版本:JDK1.8(或11) 服务器:tomcat 数据库&…...
【51单片机】程序实验56.独立按键-矩阵按键
主要参考学习资料:B站【普中官方】51单片机手把手教学视频 前置知识:C语言 单片机套装:普中STC51单片机开发板A4标准版套餐7 码字不易,求点赞收藏加关注(•ω•̥) 有问题欢迎评论区讨论~ 目录 独立按键按键介绍实验5 独立按键 矩…...
SAP 零售方案 CAR 系统的介绍与研究
前言 当今时代,零售业务是充满活力和活力的业务领域之一。每天,由于销售运营和客户行为,它都会生成大量数据。因此,公司迫切需要管理数据并从中检索见解。它将帮助公司朝着正确的方向发展他们的业务。 这就是为什么公司用来处理…...
2024 APMCM亚太数学建模C题 - 宠物行业及相关产业的发展分析和策略 完整参考论文(2)
5.2 问题一模型的建立与求解 5.2.1 分析发展情况 为了更好地理解数据的变化趋势,利用matlab通过六个子图对宠物行业中的关键变量进行了可视化展示。 图 1. 宠物数量变化展示了 猫数量、狗数量 和 总宠物数量 在 2019-2023 年间的变化趋势。结果显示:猫的数量呈逐年上升的趋…...
嵌入式的C/C++:深入理解 static、const 与 volatile 的用法与特点
目录 一、static 1、static 修饰局部变量 2、 static 修饰全局变量 3、static 修饰函数 4、static 修饰类成员 5、小结 二、const 1、const 修饰普通变量 2、const 修饰指针 3、const 修饰函数参数 4. const 修饰函数返回值 5. const 修饰类成员 6. const 与 #defi…...
【数据库设计】软件系统需要同时设计注册日志表和登录日志表吗
是的,通常情况下,注册日志表和登录日志表是分别设计的,分别记录不同类型的事件信息。 注册日志表 记录用户的注册信息、注册方式以及是否成功等内容。登录日志表 记录用户每次登录的时间、IP 地址、设备信息、登录状态等内容。 尽管这两者看…...
vim 一次注释多行 的几种方法
在 Vim 中一次注释多行是一个常见操作。可以使用以下方法根据你的具体需求选择合适的方式: 方法 1:手动插入注释符 进入正常模式: 按 Esc 确保进入正常模式。 选择需要注释的多行: 移动到第一行,按下 Ctrlv 进入可视块…...
手机无法连接服务器1302什么意思?
你有没有遇到过手机无法连接服务器,屏幕上显示“1302”这样的错误代码?尤其是在急需使用手机进行工作或联系朋友时,突然出现的连接问题无疑会带来不少麻烦。那么,什么是1302错误,它又意味着什么呢? 1302错…...
Git(一)基本使用
目录 一、使用git -v 查看安装git版本 二、使用mkdir 创建一个文件,并使用 git init 在该目录下创建一个本地仓库, 三、通过git clone命令接入线上仓库 四、使用git status查看仓库状态信息 五、利用echo写入一个文件 并使用cat进行查看 【Linux】e…...
sklearn中常用数据集简介
scikit-learn库中提供了包括分类、回归、聚类、降维等多种机器学习任务所需的常用数据集,方便进行实验和研究,它们主要被封装在sklearn.datasets中,本文对其中一些常用的数据集进行简单的介绍。 1.Iris(鸢尾花)数据集…...
LRU缓存
什么是LRU缓存? LRU(Least Recently Used)是最近最少使用算法,是操作系统中用于分页置换的算法,如果要向内存中添加分页,并且内存分页已满的情况下,就选出最近一段时间最不常用的分页进行置换(…...
.net6 使用 FreeSpire.XLS 实现 excel 转 pdf - docker 部署
FreeSpire.XLS && Aspose.Cells包都可以实现。实现过程中发现如下问题: 本地测试通过, docker部署服务器后报错: The type initializer for Spire.Xls.Core.Spreadsheet.XlsPageSetupBase threw an exception. 由于缺少依赖…...
HttpServletRequest req和前端的关系,req.getParameter详细解释,req.getParameter和前端的关系
HttpServletRequest 对象在后端和前端之间起到了桥梁的作用,它包含了来自客户端的所有请求信息。通过 HttpServletRequest 对象,后端可以获取前端发送的请求参数、请求头、请求方法等信息,并根据这些信息进行相应的处理。以下是对 HttpServle…...
[Python3] Sanic 框架构建高并发的 Web 服务
在 Python3 中使用 Sanic 框架来构建高并发的 Web 服务时,Sanic 因其异步和基于事件驱动的架构能够很好地处理高并发请求。下面是如何使用 Sanic 的一些要点和示例代码。 1. 安装 Sanic 首先确保你安装了 Sanic,可以通过以下命令安装: pip…...
5.5 W5500 TCP服务端与客户端
文章目录 1、TCP介绍2、W5500简介2.1 关键函数socketlistensendgetSn_RX_RSRrecv自动心跳包检测getSn_SR 1、TCP介绍 TCP 服务端: 创建套接字[socket]:服务器首先创建一个套接字,这是网络通信的端点。绑定套接字[bind]:服务器将…...
【Flutter】搭建Flutter开发环境,安卓开发
Flutter是谷歌开源的一个跨平台开发的框架,方便好用,这里以Windows 上构建 Flutter Android 应用为例,记录下我搭建环境时碰到的一些问题以及解决。 第一步:参考官网:开发 Android 应用 | Flutter 中文文档 - Flutter …...
【机器学习】——朴素贝叶斯模型
💻博主现有专栏: C51单片机(STC89C516),c语言,c,离散数学,算法设计与分析,数据结构,Python,Java基础,MySQL,linux…...
k8s rainbond centos7/win10 -20241124
参考 https://www.rainbond.com/ 国内一站式云原生平台 对centos7环境支持不太行 [lighthouseVM-16-5-centos ~]$ curl -o install.sh https://get.rainbond.com && bash ./install.sh 2024-11-24 09:56:57 ERROR: Ops! Docker daemon is not running. Start docke…...
ctfshow单身杯2024wp
文章目录 ctfshow单身杯2024wp签到好玩的PHPezzz_sstiez_inject ctfshow单身杯2024wp 签到好玩的PHP 考点:序列化反序列化 <?phperror_reporting(0);highlight_file(__FILE__);class ctfshow {private $d ;private $s ;private $b ;private $ctf ;public …...
深入解密 K 均值聚类:从理论基础到 Python 实践
1. 引言 在机器学习领域,聚类是一种无监督学习的技术,用于将数据集分组成若干个类别,使得同组数据之间具有更高的相似性。这种技术在各个领域都有广泛的应用,比如客户细分、图像压缩和市场分析等。聚类的目标是使得同类样本之间的…...
【代码pycharm】动手学深度学习v2-08 线性回归 + 基础优化算法
课程链接 线性回归的从零开始实现 import random import torch from d2l import torch as d2l# 人造数据集 def synthetic_data(w,b,num_examples):Xtorch.normal(0,1,(num_examples,len(w)))ytorch.matmul(X,w)bytorch.normal(0,0.01,y.shape) # 加入噪声return X,y.reshape…...
Python绘制太极八卦
文章目录 系列目录写在前面技术需求1. 图形绘制库的支持2. 图形绘制功能3. 参数化设计4. 绘制控制5. 数据处理6. 用户界面 完整代码代码分析1. rset() 函数2. offset() 函数3. taiji() 函数4. bagua() 函数5. 绘制过程6. 技术亮点 写在后面 系列目录 序号直达链接爱心系列1Pyth…...
sklearn学习
介绍:scaler:换算的意思 1. 归一化MinMaxScaler() 归一化的意思是将一堆数,如果比较离散,为了让数据更适合模型训练,将离散的数据压缩到0到1之间,以方便模型更高效优质的学习,而对数据的预处理…...
# [Unity] 【游戏开发】Unity开发基础2-Unity脚本编程基础详解
Unity脚本编程是创建互动式游戏体验的核心技能之一。本文将详细讲解Unity脚本编程的基础知识,包括变量和数据类型、程序逻辑、方法等方面,并通过实例展示如何使用这些基本知识完成简单功能的实现。 1. 新建Unity脚本的基本结构 当在Unity中创建一个脚本时,Unity会生成如下基…...
【强化学习的数学原理】第02课-贝尔曼公式-笔记
学习资料:bilibili 西湖大学赵世钰老师的【强化学习的数学原理】课程。链接:强化学习的数学原理 西湖大学 赵世钰 文章目录 一、为什么return重要?如何计算return?二、state value的定义三、Bellman公式的详细推导四、公式向量形式…...
C语言-数学基础问题
一.奇数、偶数问题 1.从键盘上输入一个整数,判断并输出它是奇数还是偶数。 //从键盘上输入一个整数,判断并输出它是奇数还是偶数。 main() {int i;printf("输入一个整数:\n");scanf("%d",&i);if(i%20)printf("它是偶数\n…...
2024算法基础公选课练习四(综合2)
一、前言 最后几个题确实有难度,这次有两题没整出来 二、题目总览 三、具体题目 3.1 问题 A: 水题系列1-B(班级排位) 思路 最暴力的思路是写线段树,然后暴力枚举两个端点,总体时间复杂度为O(n^2*logn)最坏会到1e9的数量级,可能…...
小程序-使用 iconfont 图标库报错:Failed to load font
官方默认可以忽略此错误,在清除缓存后首次刷新会显示此错误,重新渲染错误消失 解决方法: 在 iconfont 图标库选择项目设置 选中 Base64 保存,重新点击链接 -> 复制代码到项目中 操作步骤:...
鲸鱼机器人和乐高机器人的比较
鲸鱼机器人和乐高机器人各有其独特的优势和特点,家长在选择时可以根据孩子的年龄、兴趣、经济能力等因素进行综合考虑,选择最适合孩子的教育机器人产品。 优势 鲸鱼机器人 1)价格亲民:鲸鱼机器人的产品价格相对乐高更为亲民&…...
解决单元测试时找不到类名
场景: springboot单元测试mockito对mapper进行mock时: tk.mybatis.mapper.mapperexception: 无法获取实体类 XX.xx 对应的表名 分析: 使用了一个方法:Example examplenew Example(User.class); 进入源码后发现Entityhelper没…...
簡單易懂:如何在Windows系統中修改IP地址?
無論是為了連接到一個新的網路,還是為了解決網路連接問題,修改IP地址都是一個常見的操作。本文將詳細介紹如何在Windows系統中修改IP地址,包括靜態IP地址的設置和動態IP地址的獲取。 IP地址是什麼? IP地址是互聯網協議地址的簡稱…...
(详细文档!)java swing学生信息管理系统 +mysql
第一章:系统功能分析 1.1、系统简介与开发背景 学生信息管理系统是在信息化时代,特别是在教育领域中产生的。随着学校规模的不断扩大和信息化技术的不断发展,传统的纸质档案管理方式已经无法满足学校对学生信息管理的需求,因此需…...
OSG开发笔记(三十三):同时观察物体不同角度的多视图从相机技术
若该文为原创文章,未经允许不得转载 本文章博客地址:https://blog.csdn.net/qq21497936/article/details/143932273 各位读者,知识无穷而人力有穷,要么改需求,要么找专业人士,要么自己研究 长沙红胖子Qt…...
[极客大挑战 2019]BabySQL--详细解析
信息搜集 进入界面: 输入用户名为admin,密码随便输一个: 发现是GET传参,有username和password两个传参点。 我们测试一下password点位能不能注入: 单引号闭合报错,根据报错信息,我们可以判断…...
Java的字符串操作(二)(代码示例)
1. 字符串的定义 // 直接赋值方式定义字符串 String str1 "Hello World";// 使用new关键字定义字符串 String str2 new String("Hello World");// 可以通过打印对象的哈希码来查看是否是同一个对象(在一定程度上反映引用情况) Sy…...
2024-2025 ICPC, NERC, Southern and Volga Russian Regional Contest(ABCGJLN)
文章目录 N. Fixing the Expression思路code J. Waiting for...思路code C. DIY思路code L. Bridge Renovation思路code A. Bonus Project思路code G. Guess One Character思路code B. Make It Equal思路code N. Fixing the Expression 思路 签到题,只改变中间的字…...
SpringBoot(9)-Dubbo+Zookeeper
目录 一、了解分布式系统 二、RPC 三、Dubbo 四、SpringBootDubboZookeeper 4.1 框架搭建 4.2 实现RPC 一、了解分布式系统 分布式系统:由一组通过网络进行通信,为了完成共同的任务而协调工作的计算机节点组成的系统 二、RPC RPC:远程…...
现代密码学
概论 计算机安全的最核心三个关键目标(指标)/为:保密性 Confidentiality、完整性 Integrity、可用性 Availability ,三者称为 CIA三元组 数据保密性:确保隐私或是秘密信息不向非授权者泄漏,也不被非授权者使…...
websocket是什么?
一、定义 Websocket是一种在单个TCP连接上进行全双工通信的协议,它允许服务器主动向客户端推送数据,而不需要客户端不断的轮询服务器来获取数据 与http协议不同,http是一种无状态的,请求,响应模式的协议(单向通信)&a…...
idea怎么打开两个窗口,运行两个项目
今天在开发项目的时候,前端希望运行一下以前的项目,于是就需要开两个 idea 窗口,运行两个项目 这里记录一下如何设置:首先依次点击: File -> Settings -> Appearance & Behavior ->System Settings 看到如…...
aws服务--机密数据存储KMS(1)介绍和使用
在AWS(Amazon Web Services)中存储机密数据时,安全性和合规性是最重要的考虑因素。AWS 提供了多个服务和工具,帮助用户确保数据的安全性、机密性以及合规性。AWS Secrets Manager、KMS(Key Management Service)是推荐的存储机密数据的AWS服务和最佳实践。这里先看KMS。 …...
怎么编译OpenWrt镜像?-基于Widora开发板
1.准备相应的环境,我使用的环境是VMware16ubuntu20.04,如图1所示安装编译所需的依赖包; sudo apt-get install build-essential asciidoc binutils bzip2 gawk gettext git libncurses5-dev libz-dev patch python3 python2.7 unzip zlib1g-…...