Commit f6d51d9cb1bd9f56c72bad3876aa773958e62b2c
1 parent
fa30f0e2
feat:redis序列化报错
Showing
4 changed files
with
49 additions
and
75 deletions
src/main/java/com/huaheng/framework/config/RedisConfig.java
... | ... | @@ -16,6 +16,7 @@ import org.springframework.context.annotation.Configuration; |
16 | 16 | import org.springframework.context.annotation.Scope; |
17 | 17 | import org.springframework.data.redis.cache.RedisCacheConfiguration; |
18 | 18 | import org.springframework.data.redis.cache.RedisCacheManager; |
19 | +import org.springframework.data.redis.connection.RedisConnectionFactory; | |
19 | 20 | import org.springframework.data.redis.connection.RedisPassword; |
20 | 21 | import org.springframework.data.redis.connection.RedisStandaloneConfiguration; |
21 | 22 | import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; |
... | ... | @@ -32,8 +33,7 @@ import java.time.Duration; |
32 | 33 | |
33 | 34 | @Configuration |
34 | 35 | @EnableCaching // 开启缓存支持 |
35 | -public class RedisConfig extends CachingConfigurerSupport | |
36 | -{ | |
36 | +public class RedisConfig extends CachingConfigurerSupport { | |
37 | 37 | @Value("${redis.database}") |
38 | 38 | private int database; |
39 | 39 | |
... | ... | @@ -70,35 +70,31 @@ public class RedisConfig extends CachingConfigurerSupport |
70 | 70 | * 在没有指定缓存Key的情况下,key生成策略 |
71 | 71 | */ |
72 | 72 | @Bean |
73 | - public KeyGenerator keyGenerator() | |
74 | - { | |
75 | - return new KeyGenerator() | |
76 | - { | |
73 | + public KeyGenerator keyGenerator() { | |
74 | + return new KeyGenerator() { | |
77 | 75 | @Override |
78 | - public Object generate(Object target, Method method, Object... params) | |
79 | - { | |
76 | + public Object generate(Object target, Method method, Object... params) { | |
80 | 77 | StringBuffer sb = new StringBuffer(); |
81 | 78 | sb.append(target.getClass().getName()); |
82 | 79 | sb.append(method.getName()); |
83 | - for (Object obj : params) | |
84 | - { | |
80 | + for (Object obj : params) { | |
85 | 81 | sb.append(obj.toString()); |
86 | 82 | } |
87 | 83 | return sb.toString(); |
88 | 84 | } |
89 | 85 | }; |
90 | 86 | } |
87 | + | |
91 | 88 | |
92 | 89 | // 缓存管理器 使用Lettuce,和jedis有很大不同LettuceConnectionFactory lettuceConnectionFactory |
93 | 90 | @Bean |
94 | - public CacheManager cacheManager() | |
95 | - { | |
91 | + public CacheManager cacheManager() { | |
96 | 92 | // 关键点,spring cache的注解使用的序列化都从这来,没有这个配置的话使用的jdk自己的序列化,实际上不影响使用,只是打印出来不适合人眼识别 |
97 | 93 | RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() |
98 | 94 | .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))// key序列化方式 |
99 | 95 | .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(getValueSerializer()))// value序列化方式 |
100 | 96 | .disableCachingNullValues().entryTtl(timeToLive).disableCachingNullValues(); |
101 | - ;// 缓存过期时间 | |
97 | + // 缓存过期时间 | |
102 | 98 | |
103 | 99 | RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder |
104 | 100 | .fromConnectionFactory(lettuceConnectionFactory())// 默认有锁 等待锁时间为0 |
... | ... | @@ -110,8 +106,7 @@ public class RedisConfig extends CachingConfigurerSupport |
110 | 106 | * RedisTemplate配置 使用自定义redisTemplate的时候 重新定义序列化方式 LettuceConnectionFactory lettuceConnectionFactory |
111 | 107 | */ |
112 | 108 | @Bean |
113 | - public RedisTemplate<String, Object> redisTemplate() | |
114 | - { | |
109 | + public RedisTemplate<String, Object> redisTemplate() { | |
115 | 110 | // 配置redisTemplate |
116 | 111 | RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); |
117 | 112 | redisTemplate.setConnectionFactory(lettuceConnectionFactory()); |
... | ... | @@ -131,8 +126,7 @@ public class RedisConfig extends CachingConfigurerSupport |
131 | 126 | * shiroRedisTemplate配置 使用自定义shiroRedisTemplate的时候 重新定义序列化方式 LettuceConnectionFactory lettuceConnectionFactory |
132 | 127 | */ |
133 | 128 | @Bean |
134 | - public RedisTemplate<String, Object> shiroRedisTemplate() | |
135 | - { | |
129 | + public RedisTemplate<String, Object> shiroRedisTemplate() { | |
136 | 130 | // 配置redisTemplate |
137 | 131 | RedisTemplate<String, Object> shiroRedisTemplate = new RedisTemplate<String, Object>(); |
138 | 132 | shiroRedisTemplate.setConnectionFactory(lettuceConnectionFactory()); |
... | ... | @@ -148,16 +142,13 @@ public class RedisConfig extends CachingConfigurerSupport |
148 | 142 | return shiroRedisTemplate; |
149 | 143 | } |
150 | 144 | |
151 | - private RedisSerializer<String> keySerializer() | |
152 | - { | |
145 | + private RedisSerializer<String> keySerializer() { | |
153 | 146 | return new StringRedisSerializer(); |
154 | 147 | } |
155 | 148 | |
156 | - private RedisSerializer<Object> getValueSerializer() | |
157 | - { | |
149 | + private RedisSerializer<Object> getValueSerializer() { | |
158 | 150 | // 设置序列化 |
159 | - Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>( | |
160 | - Object.class); | |
151 | + Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class); | |
161 | 152 | ObjectMapper om = new ObjectMapper(); |
162 | 153 | om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); |
163 | 154 | om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); |
... | ... | @@ -167,43 +158,27 @@ public class RedisConfig extends CachingConfigurerSupport |
167 | 158 | |
168 | 159 | // 单机版配置连接参数 |
169 | 160 | @Bean |
170 | - public RedisStandaloneConfiguration redisStandaloneConfiguration() | |
171 | - { | |
161 | + public RedisStandaloneConfiguration redisStandaloneConfiguration() { | |
172 | 162 | RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); |
173 | 163 | redisStandaloneConfiguration.setDatabase(database); |
174 | 164 | redisStandaloneConfiguration.setHostName(host); |
175 | 165 | redisStandaloneConfiguration.setPort(port); |
176 | 166 | redisStandaloneConfiguration.setPassword(RedisPassword.of(password)); |
177 | - | |
178 | - // 集群版配置 | |
179 | - // RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(); | |
180 | - // String[] serverArray = clusterNodes.split(","); | |
181 | - // Set<RedisNode> nodes = new HashSet<RedisNode>(); | |
182 | - // for (String ipPort : serverArray) { | |
183 | - // String[] ipAndPort = ipPort.split(":"); | |
184 | - // nodes.add(new RedisNode(ipAndPort[0].trim(), Integer.valueOf(ipAndPort[1]))); | |
185 | - // } | |
186 | - // redisClusterConfiguration.setPassword(RedisPassword.of(password)); | |
187 | - // redisClusterConfiguration.setClusterNodes(nodes); | |
188 | - // redisClusterConfiguration.setMaxRedirects(maxRedirects); | |
189 | - | |
190 | 167 | return redisStandaloneConfiguration; |
191 | 168 | } |
192 | 169 | |
193 | 170 | /** |
194 | 171 | * 配置LettuceClientConfiguration 包括线程池配置和安全项配置 genericObjectPoolConfig common-pool2线程池GenericObjectPoolConfig |
195 | 172 | * genericObjectPoolConfig |
196 | - * | |
173 | + * | |
197 | 174 | * @return lettuceClientConfiguration |
198 | 175 | */ |
199 | 176 | @Bean |
200 | - public LettuceClientConfiguration lettuceClientConfiguration() | |
201 | - { | |
177 | + public LettuceClientConfiguration lettuceClientConfiguration() { | |
202 | 178 | LettuceClientConfiguration lettuceClientConfiguration = LettucePoolingClientConfiguration.builder() |
203 | 179 | .commandTimeout(Duration.ofMillis(timeout)).shutdownTimeout(Duration.ofMillis(200)) |
204 | 180 | .poolConfig(genericObjectPoolConfig()).build(); |
205 | - if (ssl) | |
206 | - { | |
181 | + if (ssl) { | |
207 | 182 | lettuceClientConfiguration.isUseSsl(); |
208 | 183 | } |
209 | 184 | return lettuceClientConfiguration; |
... | ... | @@ -211,8 +186,7 @@ public class RedisConfig extends CachingConfigurerSupport |
211 | 186 | |
212 | 187 | // 设置连接工厂 |
213 | 188 | @Bean |
214 | - public LettuceConnectionFactory lettuceConnectionFactory() | |
215 | - { | |
189 | + public LettuceConnectionFactory lettuceConnectionFactory() { | |
216 | 190 | return new LettuceConnectionFactory(redisStandaloneConfiguration(), lettuceClientConfiguration()); |
217 | 191 | } |
218 | 192 | |
... | ... | @@ -223,8 +197,7 @@ public class RedisConfig extends CachingConfigurerSupport |
223 | 197 | @ConfigurationProperties(prefix = "redis.lettuce.pool") |
224 | 198 | @Scope(value = "prototype") |
225 | 199 | @SuppressWarnings("rawtypes") |
226 | - public GenericObjectPoolConfig genericObjectPoolConfig() | |
227 | - { | |
200 | + public GenericObjectPoolConfig genericObjectPoolConfig() { | |
228 | 201 | return new GenericObjectPoolConfig(); |
229 | 202 | } |
230 | 203 | } |
... | ... |
src/main/java/com/huaheng/framework/redis/serializer/SerializeUtils.java
... | ... | @@ -7,37 +7,31 @@ import org.springframework.data.redis.serializer.SerializationException; |
7 | 7 | |
8 | 8 | import java.io.*; |
9 | 9 | |
10 | -public class SerializeUtils<T> implements RedisSerializer<T> | |
11 | -{ | |
10 | +public class SerializeUtils<T> implements RedisSerializer<T> { | |
12 | 11 | private static Logger logger = LoggerFactory.getLogger(SerializeUtils.class); |
13 | 12 | |
14 | - public static boolean isEmpty(byte[] data) | |
15 | - { | |
13 | + public static boolean isEmpty(byte[] data) { | |
16 | 14 | return (data == null || data.length == 0); |
17 | 15 | } |
18 | 16 | |
19 | 17 | /** |
20 | 18 | * 序列化 |
21 | - * | |
19 | + * | |
22 | 20 | * @param t |
23 | 21 | * @return |
24 | 22 | * @throws SerializationException |
25 | 23 | */ |
26 | 24 | @Override |
27 | - public byte[] serialize(T t) throws SerializationException | |
28 | - { | |
25 | + public byte[] serialize(T t) throws SerializationException { | |
29 | 26 | byte[] result = null; |
30 | 27 | |
31 | - if (t == null) | |
32 | - { | |
28 | + if (t == null) { | |
33 | 29 | return new byte[0]; |
34 | 30 | } |
35 | 31 | try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream(128); |
36 | - ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteStream)) | |
37 | - { | |
32 | + ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteStream)) { | |
38 | 33 | |
39 | - if (!(t instanceof Serializable)) | |
40 | - { | |
34 | + if (!(t instanceof Serializable)) { | |
41 | 35 | throw new IllegalArgumentException( |
42 | 36 | SerializeUtils.class.getSimpleName() + " requires a Serializable payload " |
43 | 37 | + "but received an object of type [" + t.getClass().getName() + "]"); |
... | ... | @@ -46,9 +40,7 @@ public class SerializeUtils<T> implements RedisSerializer<T> |
46 | 40 | objectOutputStream.writeObject(t); |
47 | 41 | objectOutputStream.flush(); |
48 | 42 | result = byteStream.toByteArray(); |
49 | - } | |
50 | - catch (Exception ex) | |
51 | - { | |
43 | + } catch (Exception ex) { | |
52 | 44 | logger.error("Failed to serialize", ex); |
53 | 45 | } |
54 | 46 | return result; |
... | ... | @@ -56,28 +48,23 @@ public class SerializeUtils<T> implements RedisSerializer<T> |
56 | 48 | |
57 | 49 | /** |
58 | 50 | * 反序列化 |
59 | - * | |
51 | + * | |
60 | 52 | * @param bytes |
61 | 53 | * @return |
62 | 54 | * @throws SerializationException |
63 | 55 | */ |
64 | 56 | @SuppressWarnings("unchecked") |
65 | 57 | @Override |
66 | - public T deserialize(byte[] bytes) throws SerializationException | |
67 | - { | |
58 | + public T deserialize(byte[] bytes) throws SerializationException { | |
68 | 59 | T result = null; |
69 | - if (isEmpty(bytes)) | |
70 | - { | |
60 | + if (isEmpty(bytes)) { | |
71 | 61 | return null; |
72 | 62 | } |
73 | 63 | ObjectInputStream objectInputStream; |
74 | - try | |
75 | - { | |
64 | + try { | |
76 | 65 | objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes)); |
77 | 66 | result = (T) objectInputStream.readObject(); |
78 | - } | |
79 | - catch (Exception e) | |
80 | - { | |
67 | + } catch (Exception e) { | |
81 | 68 | logger.error("Failed to deserialize", e); |
82 | 69 | } |
83 | 70 | return result; |
... | ... |
src/main/java/com/huaheng/mobile/receipt/MobileBatchReceiptController.java
... | ... | @@ -538,7 +538,7 @@ public class MobileBatchReceiptController { |
538 | 538 | |
539 | 539 | @PostMapping("/callBox") |
540 | 540 | @ApiOperation("移动端呼叫料盒") |
541 | - @ApiLogger(apiName = "wcs调用wms分配库位", from = "WCS") | |
541 | + @ApiLogger(apiName = "PDA呼叫料盒", from = "WCS") | |
542 | 542 | @Log(title = "移动端呼叫料盒", action = BusinessType.OTHER) |
543 | 543 | @Transactional(rollbackFor = Exception.class) |
544 | 544 | public AjaxResult callBox(@RequestBody @ApiParam(value = "物料号") Map<String, String> param) { |
... | ... |
src/main/java/com/huaheng/pc/inventory/inventoryTransaction/domain/vo/InventoryRankVO.java
... | ... | @@ -9,14 +9,28 @@ import java.math.BigDecimal; |
9 | 9 | @Data |
10 | 10 | @Builder |
11 | 11 | public class InventoryRankVO implements Serializable { |
12 | + // 空构造函数 | |
13 | + public InventoryRankVO() { | |
14 | + } | |
15 | + | |
16 | + // 带参数的构造函数 | |
17 | + public InventoryRankVO(String materialCode, Integer inQty, Integer outQty, String materialName, String materialSpec, String materialUnit, BigDecimal taskQty) { | |
18 | + this.materialCode = materialCode; | |
19 | + this.inQty = inQty; | |
20 | + this.outQty = outQty; | |
21 | + this.materialName = materialName; | |
22 | + this.materialSpec = materialSpec; | |
23 | + this.materialUnit = materialUnit; | |
24 | + this.taskQty = taskQty; | |
25 | + } | |
12 | 26 | |
13 | 27 | private static final long serialVersionUID = 2903643973589589715L; |
14 | 28 | |
15 | 29 | private String materialCode; |
16 | 30 | private Integer inQty; |
17 | 31 | private Integer outQty; |
18 | - private BigDecimal taskQty; | |
19 | 32 | private String materialName; |
20 | 33 | private String materialSpec; |
21 | 34 | private String materialUnit; |
35 | + private BigDecimal taskQty; | |
22 | 36 | } |
... | ... |