找不到指令
默认情况下,缺失命令通过以下方式处理命令非找到结果处理器并输出一个简单的消息:
shell:>missing
No command found for 'missing'
内部命令非找到结果处理器是使用命令非找到消息提供者这是一个简单的函数,取提供者上下文并回复短信
消息。以下是自定义消息提供商可能的示例。
class CustomProvider implements CommandNotFoundMessageProvider {
@Override
public String apply(ProviderContext context) {
// parsed commands without options
List<String> commands = context.commands();
// actual error, usually CommandNotFound exception
Throwable error = context.error();
// access to registrations at this time
Map<String, CommandRegistration> registrations = context.registrations();
// raw text input from a user
String text = context.text();
return "My custom message";
}
}
可以通过将其定义为“豆子”来改变这种实现。
@Bean
CommandNotFoundMessageProvider provider1() {
return new CustomProvider();
}
命令非找到结果处理器是一个功能性接口,因此它可以
写作者为一个lambda。
@Bean
CommandNotFoundMessageProvider provider2() {
return ctx -> "My custom message";
}