java – 如何使用spring shell在spring boot web应用程序中构建控制台命令?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何使用spring shell在spring boot web应用程序中构建控制台命令?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含9167字,纯文字阅读大概需要14分钟。
内容图文
我使用spring boot web starter创建了restfull web应用程序,效果很好.我可以通过网址访问它.
但是我需要创建可以在后端计算和存储一些值的控制台命令.我希望能够手动或通过bash脚本运行控制台命令.
我找不到任何关于如何在spring boot web应用程序中集成spring-shell项目的文档.
此外,在弹簧启动器启动器https://start.spring.io/中没有选择弹簧壳依赖性的选项
1)webapp和console需要是两个独立的应用程序吗?我需要单独部署它们吗?
2)是否可以在同一个应用程序中部署Web应用程序并运行控制台命令?
3)在shell和Web应用程序之间共享公共代码(模型,服务,实体,业务逻辑)的最佳方法是什么?
有人可以帮忙吗?
解决方法:
这里有两个选项:
(1) Rest API called from the command line
您可以创建一个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脚本中.
(2) Use spring-boot-remote-shell (deprecated)
虽然它主要用于监视/管理目的,但您可以使用spring-boot-remote-shell.
依赖
您需要以下依赖项来启用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>
Groovy脚本:
在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脚本中获取Spring bean(来源:https://stackoverflow.com/a/24300534/641627):
BeanFactory beanFactory = (BeanFactory) context.getAttributes().get("spring.beanfactory");
MyController myController = beanFactory.getBean(MyController.class);
启动SpringBootApp
使用类路径上的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并执行自定义
(3) How to use arguments and options (to answer question in comments)
参数
要使用参数,您可以查看documentation:
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
子命令(或选项)
仍然从他们的documentation:
@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
内容总结
以上是互联网集市为您收集整理的java – 如何使用spring shell在spring boot web应用程序中构建控制台命令?全部内容,希望文章能够帮你解决java – 如何使用spring shell在spring boot web应用程序中构建控制台命令?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。