【问题标题】:Can you use (any) Spring's functionality outside of Spring (Boot)?您可以在 Spring(Boot)之外使用(任何)Spring 的功能吗?
【发布时间】:2016-12-21 07:27:41
【问题描述】:

我刚刚使用 Spring Boot 构建了一个 RESTful Web 服务。

我现在想利用 RESTful Web 服务并开始通过构建 Java 控制台应用程序来调用它(最终添加 GUI 和安全性)。

我想知道是否可以在 Spring(引导)环境之外使用任何 Spring 功能并在我的 java 控制台应用程序中使用它?例如,我可以在我的非 Spring Java 应用程序中使用 Spring 的 RestTemplate 来进行 REST api 调用吗?我是 Spring 的新手,我想尽可能地接近 Spring。我想你不能,但我只是想确定一下。

如果不可能,我知道您可以使用 Spring 创建非 Web 应用程序。是否可以集成GUI?可能不是最佳实践,只是探索什么是可能的和常规的。

【问题讨论】:

  • 是的,你可以新建类实例然后使用它,RestTemplate只是让http客户端更容易。
  • 尝试添加所需的 Spring 依赖项而不是 Spring Boot。要进行 REST api 调用,您可以使用 bash 中的 curl,从 Java 代码中使用 google hoe。

标签: spring rest spring-boot


【解决方案1】:

Spring Boot 不以任何方式耦合到应用程序类型。您可以使用它运行仅命令行应用程序、批处理应用程序、Web 应用程序或任何其他类型的应用程序。您甚至可以从 Spring Boot 的自动配置中受益。

对于RestTemplate,您可能希望直接导入spring-web 而不是spring-boot-starter-web。或者您可以添加启动器并排除嵌入式容器 (spring-boot-starter-tomcat)。在这种情况下,Spring Boot 将自动适应并且不会启动嵌入式 Web 服务器。

【讨论】:

  • 嗨斯蒂芬。不错的答案!但是我在我的非 Spring Boot java 项目中添加了实现 'org.springframework.boot:spring-boot-web' 并且它没有找到依赖项。请检查我的 gradle.file 并帮助 `apply plugin: 'java' apply plugin: 'maven' sourceCompatibility = 1.8 // java 8 targetCompatibility = 1.8 repositories { mavenCentral() } dependencies { // JSON 处理注释实现 'com.fasterxml. jackson.core:jackson-annotations:2.4.1' implementation 'org.springframework.boot:spring-boot-web' compile 'com.google.guava:guava:18.0' } `
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-15
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
  • 2021-01-15
  • 2020-01-30
  • 2019-02-21
相关资源
最近更新 更多