springshell自定义Stacktrace命令

springshell工程如在国内环境时需要中文提示堆栈信息,可自定义Stacktrace命令:

复制代码
import org.jline.terminal.Terminal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.shell.result.ThrowableResultHandler;
import org.springframework.shell.standard.ShellCommandGroup;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.commands.Stacktrace;

@ShellComponent
@ShellCommandGroup(value = "SpringShellDemo Commands") // 自定义分组名
public class StacktraceCommand implements Stacktrace.Command{

    public interface Command {}

    @Autowired
    @Lazy
    private Terminal terminal;

    @Autowired
    private ThrowableResultHandler throwableResultHandler;

    // 自定义提示信息
    @ShellMethod(key = ThrowableResultHandler.DETAILS_COMMAND_NAME, value = "Display the full stacktrace of the last error.")
    public void stacktrace() {
        if (throwableResultHandler.getLastError() != null) {
            throwableResultHandler.getLastError().printStackTrace(terminal.writer());
        }
    }
}
复制代码

启动类中禁用springshell内置命令:

复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.util.StringUtils;

@SpringBootApplication
public class SpringShellDemoApplication {

    public static void main(String[] args) {String[] disabledCommands = {"--spring.shell.command.stacktrace.enabled=false"}; 
    String[] fullArgs
= StringUtils.concatenateStringArrays(args, disabledCommands); SpringApplication.run(SpringShellDemoApplication.class, fullArgs);
  }
}
复制代码

 

posted @   刀霸汉  阅读(72)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示