Jackson 对象与json数据互转工具类JacksonUtil

ops/2024/11/26 7:57:45/

下面是一个基于 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


http://www.ppmy.cn/ops/136781.html

相关文章

反向代理服务器的用途

代理服务器在网络中扮演着重要的角色&#xff0c;它们可以优化流量、保护服务器以及提高安全性。在代理服务器中&#xff0c;反向代理服务器是一种特殊类型&#xff0c;常用于Web服务器前&#xff0c;它具备多种功能&#xff0c;能够确保网络流量的顺畅传输。那么&#xff0c;让…

【Pytest+Yaml+Allure】实现接口自动化测试框架

一、框架思想 requestsyamlpytestallure实现接口自动化框架。结合数据驱动和分层思想&#xff0c;将代码与数据分离&#xff0c;易维护&#xff0c;易上手。使用yaml编写编写测试用例&#xff0c;利用requests库发送请求&#xff0c;使用pytest管理用例&#xff0c;allure生成…

BERT的基本理念

BERT的基本理念 BERT的基本理念&#xff1a; word2vec是一类生成词向量的模型的总称。这类模型多为浅层或者双层的神经网络&#xff0c;通过训练建立词在语言空间中的向量关系。 BERT是Bidirectional Encoder Representations from Transformers的缩写&#xff0c;意为多Tran…

微服务02

微服务02 1.网关路由 1.1.认识网关 什么是网关&#xff1f; 顾明思议&#xff0c;网关就是网络的关口。数据在网络间传输&#xff0c;从一个网络传输到另一网络时就需要经过网关来做数据的路由和转发以及数据安全的校验。 更通俗的来讲&#xff0c;网关就像是以前园区传达…

Python入门(13)--并发编程

Python并发编程&#xff1a;从入门到实践 &#x1f680; 1. 多线程编程基础 &#x1f9f5; 多线程是实现并发的重要方式&#xff0c;Python提供了threading模块来支持多线程编程。 1.1 基本线程操作 import threading import time from typing import List, Callableclass …

kafka如何知道哪个消费者消费哪个分区?

在Kafka中&#xff0c;消费者和分区之间的分配是通过一个称为“消费者组协调器”&#xff08;Consumer Group Coordinator&#xff09;的组件来管理的。 以下是Kafka如何确定哪个消费者消费哪个分区的步骤&#xff1a; 消费者加入消费者组&#xff1a; 当消费者启动时&#xf…

Java爬虫:数据采集的强大工具

引言 在信息爆炸的今天&#xff0c;数据已成为企业决策的重要依据。无论是市场趋势分析、用户行为研究还是竞争对手监控&#xff0c;都离不开对海量数据的收集和分析。Java作为一种成熟且功能强大的编程语言&#xff0c;其在数据采集领域——尤其是爬虫技术的应用——展现出了…

springmvc-04-Controller及RestFul

4. Controller及RestFul 4.1. 控制器Controller 控制器复杂提供访问应用程序的行为&#xff0c;通常通过接口定义或注解定义两种方法实现。控制器负责解析用户的请求并将其转换为一个模型。在Spring MVC中一个控制器类可以包含多个方法在Spring MVC中&#xff0c;对于Control…