大佬教程收集整理的这篇文章主要介绍了如何使用Spring Shell在Spring Boot Web应用程序中构建控制台命令?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是2个选项:
您@R_618_9838@一个Spring @RestController
,然后从命令行调用它?
curl -X POST -i -H "Content-type: application/Json" -c cookies.txt -X POST http://hostname:8080/service -d '
{
"fIEld":"value",
"fIEld2":"value2"
}
'
您可以轻松地将其嵌入一个不错的sHell脚本中。
尽管它主要用于监视/管理目的,但是您可以使用spring-boot-remote-sHell。
您需要以下依赖项才能启用远程外壳程序:
<dependency>
<groupID>org.springframework.boot</groupID>
<artifactID>spring-boot-starter-remote-sHell</artifactID>
</dependency>
<dependency>
<groupID>org.crsh</groupID>
<artifactID>crsh.sHell.telnet</artifactID>
<version>1.3.0-beta2</version>
</dependency>
:
在中添加以下脚本src/main/resources/custom.groovy
:
package commands
import org.crsh.cli.Command
import org.crsh.cli.Usage
import org.crsh.command.InvocationContext
class custom {
@Usage("Custom command")
@Command
def main(InvocationContext context) {
return "Hello"
}
}
要从该groovy脚本中获取Springbean:
beanfactory beanfactory = (beanfactory) context.getAttributes().get("spring.beanfactory");
MyController myController = beanfactory.getBean(MyController.class);
在类路径上使用spring-boot-remote-sHell时,Spring Boot应用程序在端口5000上侦听(默认情况下)。您现在可以执行以下操作:
$ telnet localhost 5000
Trying ::1...
Connected to localhost.
Escape character is '^]'.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.5.RELEASE)
您可以键入Help
以查看可用命令的列表:
name DESCRIPTION
autoconfig display auto configuration report from ApplicationContext
beans display beans in ApplicationContext
cron manages the cron plugin
custom Custom command
dashboard
egrep search file(s) for lines that match a pattern
endpoint Invoke actuator endpoints
env display the term env
filter A filter for a stream of map
Help provIDes basic Help
java varIoUs java language commands
jmx Java Management Extensions
jul java.util.logging commands
jvm JVM informations
less opposite of more
log logging commands
mail interact with emails
man format and display the on-line manual pages
metrics display metrics provIDed by Spring Boot
sHell sHell related command
sleep sleep for some time
sort Sort a map
system vm system propertIEs commands
thread JVM thread commands
我们的自定义命令已列出(自上而下的第四个),您可以调用它:
> custom
Hello
因此,从本质上讲,您的crontab将telnet 5000
执行custom
要使用参数,可以看一下文档:
class date {
@Usage("show the current time")
@Command
Object main(
@Usage("the time format")
@Option(names=["f","format"])
String format) {
if (format == null)
format = "EEE MMM d HH:mm:ss z yyyy";
def date = new Date();
return date.format(format);
}
}
% date -h
% usage: date [-h | --Help] [-f | --format]
% [-h | --Help] command usage
% [-f | --format] the time format
% date -f yyyymMdd
仍然从他们的文档中:
@Usage("JDBC connection")
class jdbc {
@Usage("connect to database with a JDBC connection String")
@Command
public String connect(
@Usage("The username")
@Option(names=["u","username"])
String user,
@Usage("The password")
@Option(names=["p","password"])
String password,
@Usage("The extra propertIEs")
@Option(names=["propertIEs"])
PropertIEs propertIEs,
@Usage("The connection String")
@Argument
String connectionString) {
...
}
@Usage("close the current connection")
@Command
public String close() {
...
}
}
% jdbc connect jdbc:derby:memory:EmbeddedDB;create=true
最后一条命令执行:
jdbc
connect
jdbc:derby:memory:EmbeddedDB;create=true
以下内容包括:
编码:
package commands
import org.crsh.cli.Command
import org.crsh.cli.Usage
import org.crsh.command.InvocationContext
import org.springframework.beans.factory.beanfactory
import com.alexbt.goodIEs.MyBean
class Saymessage {
String message;
Saymessage(){
this.message = "Hello";
}
@Usage("Default command")
@Command
def main(InvocationContext context, @Usage("A Parameter") @Option(names=["p","param"]) String param) {
beanfactory beanfactory = (beanfactory) context.getAttributes().get("spring.beanfactory");
MyBean bean = beanfactory.getBean(MyBean.class);
return message + " " + bean.getValue() + " " + param;
}
@Usage("Hi subcommand")
@Command
def hi(InvocationContext context, @Usage("A Parameter") @Option(names=["p","param"]) String param) {
beanfactory beanfactory = (beanfactory) context.getAttributes().get("spring.beanfactory");
MyBean bean = beanfactory.getBean(MyBean.class);
return "Hi " + bean.getValue() + " " + param;
}
}
> saymsg -p Johnny
> Hello my frIEnd Johnny
> saymsg hi -p Johnny
> Hi my frIEnd Johnny
我已经使用spring boot web starter创建了restfull Web应用程序,效果很好。我可以通过网址访问它。
但是我需要创建控制台命令,该命令可以在后端计算和存储一些值。我希望能够手动或通过bash脚本运行控制台命令。
我找不到任何有关如何在Spring Boot Web应用程序中集成spring-sHell项目的文档。
1)webapp和控制台是否需要是两个单独的应用程序,我需要分别部署它们?
2)是否可以在同一应用程序中部署Web应用程序并运行控制台命令?
3)在SHell和Web应用程序之间共享通用代码(模型,服务,实体,业务逻辑)的最佳方法是什么?
有人可以帮忙吗?
以上是大佬教程为你收集整理的如何使用Spring Shell在Spring Boot Web应用程序中构建控制台命令?全部内容,希望文章能够帮你解决如何使用Spring Shell在Spring Boot Web应用程序中构建控制台命令?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。