Springboot中常见问题记录

本文档详述了Java应用程序在不同环境中遇到的常见问题及其解决方案,包括启动错误、类路径问题、定时任务配置及AOP与缓存框架兼容性等,并提供了一个用于消息序列化的Gson对象转换工具类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.启动错误

1.1 错误: 找不到或无法加载主类

1.2 找不到主清单

2.找不到类

2.1 java -jar 启动失败

3.定时任务

4.AOP和JetCache同时使用出现的问题

gson对象转换


1.启动错误

1.1 错误: 找不到或无法加载主类

解决方法:

mvn clean package

1.2 找不到主清单

在CCE容器部署时候发现日志一直打印这个,然后容器无法正常启动

no main manifest attribute, in delivery-ocpx-1.0.0.jar

问题原因:注意repackage

            <plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>2.6.6</version>
				<configuration>
					<fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
				</configuration>
                <!-- 加上这个就ok了 -->
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

2.找不到类

2.1 java -jar 启动失败

问题描述:IDEA可以正常启动,打包后运行出现java.lang.ClassNotFoundException

详细: 

为什么我能搞出这么大的BUG

问题原因:

解决方法: 

3.定时任务

问题描述:

1.通过@Scheduler启动的定时任务在本地执行,服务器部署也执行,但是在docker容器中不执行

问题原因:

1.这个定时任务是单线程,需要配置任务线程池

@Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(50);
        return taskScheduler;
    }

2.时区问题,本地时区和容器的时区不一样

@Scheduled(cron = "0 0/1 * * * ?", zone = "Asia/Shanghai")

时区问题在使用@Scheduled注解时保持和docker容器里面的时区一致

然后问题解决

4.AOP和JetCache同时使用出现的问题

java.lang.IllegalStateException: No MethodInvocation found: Check that an AOP invocation is in progress and that the ExposeInvocationInterceptor is upfront in the interceptor chain. Specifically, note that advices with order HIGHEST_PRECEDENCE will execute before ExposeInvocationInterceptor! In addition, ExposeInvocationInterceptor and ExposeInvocationInterceptor.currentInvocation() must be invoked from the same thread

解决办法:加入 @Order注解

gson对象转换

说明:在使用redis做消息发布订阅时候,B服务消费A服务发送的消息,进行序列化发现B服务实体对象中存在Date类型的createTime和updateTime字段,正常使用的转换器无法成功序列化

自己使用的序列化工具类
 

package com.hhmt.delivery.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hhmt.delivery.adapter.GsonTypeAdapter;
import com.hhmt.delivery.constant.DateParams;
import com.hhmt.delivery.continer.ErrorCode;
import com.hhmt.delivery.core.domain.ResultVo;
import com.hhmt.delivery.core.page.TableDataInfo;
import com.hhmt.delivery.utils.exception.ServiceCustomerException;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

/**
 * @author huachun
 * @version 1.0
 * @description: TODO
 * @email huachun_w@163.com
 * @date 2022-08-23 11:16
 */
@Log4j2
@Component
public class TypeCovertUtils {    

    /**
     * @param data  数据
     * @param clazz 目标类型
     * @return java.util.List<T>
     * @description: 转换List集合类型
     * 示例: typeCovertUtils.covertListType(baiduAdxVo.getResponse(), HhAdxAdvertiserInfoVo.class);
     * @author huachun
     * @email huachun_w@163.com
     * @date 2023/1/5 18:58
     */
    public <T, K> List<T> covertListType(K data, Class<T> clazz) {
        Gson gson = new GsonBuilder().setDateFormat(DateFormatContant.YYYY_MM_DD_HH_MM_SS).registerTypeAdapter(new TypeToken<List<T>>() {
        }.getType(), new GsonTypeAdapter()).create();
        String jsonStr = gson.toJson(data);
        return gson.fromJson(jsonStr, new TypeToken<List<T>>() {
        }.getType());

    }

    

}

通过 Gson gson = new GsonBuilder().setDateFormat(DateFormatContant.YYYY_MM_DD_HH_MM_SS) 方式设置Date属性格式,完美解决问题

原文参考:https://wenku.youkuaiyun.com/answer/056d2ad1b8444c83a0d4949eec180b76

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值