默认情况下,缺少的命令是通过处理的,并输出一条简单的消息:CommandNotFoundResultHandler
shell:>missing
No command found for 'missing'
内部正在使用,这是一个简单的函数,获取并返回文本
消息。下面是自定义消息提供程序的示例。CommandNotFoundResultHandler
CommandNotFoundMessageProvider
ProviderContext
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 来更改此实现。
@Bean
CommandNotFoundMessageProvider provider1() {
return new CustomProvider();
}
CommandNotFoundResultHandler
是一个功能接口,因此它可以
写成 lambda。
@Bean
CommandNotFoundMessageProvider provider2() {
return ctx -> "My custom message";
}