test
			
			
		
		
							parent
							
								
									b1a99cced7
								
							
						
					
					
						commit
						006ada7ca6
					
				| @ -0,0 +1,35 @@ | |||||||
|  | package com.glxp.api.config; | ||||||
|  | 
 | ||||||
|  | import com.fasterxml.jackson.databind.ObjectMapper; | ||||||
|  | import com.fasterxml.jackson.databind.module.SimpleModule; | ||||||
|  | import com.glxp.api.config.serializer.*; | ||||||
|  | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||||||
|  | import org.springframework.context.annotation.Bean; | ||||||
|  | import org.springframework.context.annotation.Configuration; | ||||||
|  | import org.springframework.context.annotation.Primary; | ||||||
|  | import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; | ||||||
|  | 
 | ||||||
|  | import java.time.LocalDate; | ||||||
|  | import java.time.LocalDateTime; | ||||||
|  | 
 | ||||||
|  | @Configuration | ||||||
|  | public class JacksonConfig { | ||||||
|  | 
 | ||||||
|  |     @Bean | ||||||
|  |     @Primary | ||||||
|  | //    @ConditionalOnMissingBean(ObjectMapper.class)
 | ||||||
|  |     public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { | ||||||
|  |         ObjectMapper objectMapper = builder.createXmlMapper(false).build(); | ||||||
|  |         SimpleModule simpleModule = new SimpleModule(); | ||||||
|  | 
 | ||||||
|  |         simpleModule.addSerializer(Long.class, new LongJsonSerializer()); | ||||||
|  |         simpleModule.addDeserializer(Long.class, new LongJsonDeserializer()); | ||||||
|  |         simpleModule.addSerializer(LocalDate.class, new LocalDateJsonSerializer()); | ||||||
|  |         simpleModule.addDeserializer(LocalDate.class, new LocalDateJsonDeserializer()); | ||||||
|  |         simpleModule.addSerializer(LocalDateTime.class, new LocalDateTimeJsonSerializer()); | ||||||
|  |         simpleModule.addDeserializer(LocalDateTime.class, new LocalDateTimeJsonDeserializer()); | ||||||
|  | 
 | ||||||
|  |         objectMapper.registerModule(simpleModule); | ||||||
|  |         return objectMapper; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,17 @@ | |||||||
|  | package com.glxp.api.config.serializer; | ||||||
|  | 
 | ||||||
|  | import cn.hutool.json.JSONNull; | ||||||
|  | import com.fasterxml.jackson.core.JsonGenerator; | ||||||
|  | import com.fasterxml.jackson.databind.JsonSerializer; | ||||||
|  | import com.fasterxml.jackson.databind.SerializerProvider; | ||||||
|  | import org.springframework.boot.jackson.JsonComponent; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | 
 | ||||||
|  | @JsonComponent | ||||||
|  | public class JsonNullSerializer extends JsonSerializer<JSONNull> { | ||||||
|  |     @Override | ||||||
|  |     public void serialize(JSONNull jsonNull, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | ||||||
|  |         jsonGenerator.writeNull(); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,31 @@ | |||||||
|  | package com.glxp.api.config.serializer; | ||||||
|  | 
 | ||||||
|  | import cn.hutool.core.date.DatePattern; | ||||||
|  | import cn.hutool.core.date.LocalDateTimeUtil; | ||||||
|  | import com.fasterxml.jackson.core.JsonParser; | ||||||
|  | import com.fasterxml.jackson.databind.DeserializationContext; | ||||||
|  | import com.fasterxml.jackson.databind.JsonDeserializer; | ||||||
|  | import lombok.extern.slf4j.Slf4j; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.time.LocalDate; | ||||||
|  | import java.time.format.DateTimeFormatter; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * 字符串转Long类型 | ||||||
|  |  */ | ||||||
|  | @Slf4j | ||||||
|  | public class LocalDateJsonDeserializer extends JsonDeserializer<LocalDate> { | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||||||
|  |         String value = p.getText(); | ||||||
|  |         try { | ||||||
|  |             return value == null ? null : LocalDateTimeUtil.parseDate(value, DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)); | ||||||
|  |         } catch (Exception e) { | ||||||
|  |             log.error("日期格式解析错误", e); | ||||||
|  |             return null; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,31 @@ | |||||||
|  | package com.glxp.api.config.serializer; | ||||||
|  | 
 | ||||||
|  | import cn.hutool.core.date.DatePattern; | ||||||
|  | import cn.hutool.core.date.LocalDateTimeUtil; | ||||||
|  | import com.fasterxml.jackson.core.JsonParser; | ||||||
|  | import com.fasterxml.jackson.databind.DeserializationContext; | ||||||
|  | import com.fasterxml.jackson.databind.JsonDeserializer; | ||||||
|  | import lombok.extern.slf4j.Slf4j; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.time.LocalDateTime; | ||||||
|  | import java.time.format.DateTimeFormatter; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * 字符串转Long类型 | ||||||
|  |  */ | ||||||
|  | @Slf4j | ||||||
|  | public class LocalDateTimeJsonDeserializer extends JsonDeserializer<LocalDateTime> { | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||||||
|  |         String value = p.getText(); | ||||||
|  |         try { | ||||||
|  |             return value == null ? null : LocalDateTimeUtil.parse(value, DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)); | ||||||
|  |         } catch (Exception e) { | ||||||
|  |             log.error("日期格式解析错误", e); | ||||||
|  |             return null; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,22 @@ | |||||||
|  | package com.glxp.api.config.serializer; | ||||||
|  | 
 | ||||||
|  | import com.fasterxml.jackson.core.JsonParser; | ||||||
|  | import com.fasterxml.jackson.databind.DeserializationContext; | ||||||
|  | import com.fasterxml.jackson.databind.JsonDeserializer; | ||||||
|  | import lombok.extern.slf4j.Slf4j; | ||||||
|  | 
 | ||||||
|  | import java.io.IOException; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * 字符串转Long类型 | ||||||
|  |  */ | ||||||
|  | @Slf4j | ||||||
|  | public class LongJsonDeserializer extends JsonDeserializer<Long> { | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||||||
|  |         String value = p.getText(); | ||||||
|  |         return value == null ? null : Long.parseLong(value); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -1,34 +1,18 @@ | |||||||
| package com.glxp.api.util; | package com.glxp.api.util; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| import cn.hutool.core.lang.Snowflake; |  | ||||||
| import cn.hutool.core.net.NetUtil; |  | ||||||
| import cn.hutool.core.util.IdUtil; | import cn.hutool.core.util.IdUtil; | ||||||
| import com.fasterxml.jackson.annotation.JsonFormat; | import cn.hutool.core.util.RandomUtil; | ||||||
| import org.springframework.stereotype.Component; |  | ||||||
| 
 | 
 | ||||||
| import javax.annotation.PostConstruct; |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * @Author tan |  | ||||||
|  * @Date 2021/10/16 16:23 |  | ||||||
|  */ |  | ||||||
| @Component |  | ||||||
| public class SnowflakeUtil { | public class SnowflakeUtil { | ||||||
|     @JsonFormat(shape = JsonFormat.Shape.STRING) | 
 | ||||||
|     private long workerId = 0;//为终端ID
 |     /** | ||||||
|     private long dataCenterId = 1;//数据中心ID
 |      * 获取雪花id | ||||||
|     private Snowflake snowflake = IdUtil.createSnowflake(workerId,dataCenterId); |      */ | ||||||
|     @PostConstruct |     public static Long getId() { | ||||||
|     public void init(){ |         int workId = RandomUtil.randomInt(0, 31); | ||||||
|         workerId = NetUtil.ipv4ToLong(NetUtil.getLocalhostStr()); |         int dataCenterId = RandomUtil.randomInt(0, 31); | ||||||
|     } |         return IdUtil.getSnowflake(workId, dataCenterId).nextId(); | ||||||
|     public synchronized String snowflakeId(){ |  | ||||||
|         return String.valueOf(snowflake.nextId()); |  | ||||||
|     } |  | ||||||
|     public synchronized long snowflakeId(long workerId,long dataCenterId){ |  | ||||||
|         Snowflake snowflake = IdUtil.createSnowflake(workerId, dataCenterId); |  | ||||||
|         return snowflake.nextId(); |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
					Loading…
					
					
				
		Reference in New Issue