【问题标题】:Using Spring Boot without Maven在没有 Maven 的情况下使用 Spring Boot
【发布时间】:2020-04-24 16:27:25
【问题描述】:

我正在尝试编写一个简单的 Java 程序,将 Hello World 显示为微服务。我正在使用 Spring Boot,但由于我公司的安全原因,我无法使用 Maven。因此,我别无选择,只能下载 jar 文件并将它们添加到我的项目中。我已经这样做了,并确保我使用的是最新的 jar 版本。当我运行我的程序时,它没有显示任何错误,但是 Tomcat 服务器没有启动并且我没有看到“Hello World”消息。以下是我的代码:

package com.tutorials;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class HelloWorld {

    @RequestMapping("/")
    //http://localhost:port/

    String hello()
    {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception{

        SpringApplication.run(HelloWorld.class,args);
        System.out.println("done");


    }



}

下面是输出:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.2.RELEASE)

2020-01-07 11:10:44.563  INFO 13224 --- [           main] com.tutorials.HelloWorld                 : Starting HelloWorld on HQTPM00184606D with PID 13224 (started by z.a in C:\Users\Z\Desktop\Workspace\HelloWorld_Microservice)
2020-01-07 11:10:44.566  INFO 13224 --- [           main] com.tutorials.HelloWorld                 : No active profile set, falling back to default profiles: default
2020-01-07 11:10:44.938  INFO 13224 --- [           main] com.tutorials.HelloWorld                 : Started HelloWorld in 0.703 seconds (JVM running for 1.096)
done

以下是我正在使用的jar文件:

为什么 Tomcat 没有启动以及我在这里缺少哪些依赖项?谢谢。

【问题讨论】:

  • 您是否尝试过将hello() 设为公共方法?您似乎还缺少类级别的 @SpringBootApplication 注释。
  • 旁注:“由于我公司的安全原因,我无法使用 Maven”-> 我真的会挑战这一点,因为如果你不能使用正确的构建,你的生活将会非常艰难像 Maven 或 Gradle 这样的工具。如果他们不想从 Internet 下载,他们可以安装公司存储库,例如 NexusArtifactory
  • 您确实需要 Maven 或 Gradle 之类的东西来使用 Spring Boot。它创建的 jar/war 具有特殊的包装和专门的启动方式。如果没有 Maven 或 Gradle,您需要确保自己也复制它,否则交付的工件将无法工作。

标签: java spring-boot maven tomcat microservices


【解决方案1】:

spring-boot-starter-web 2.2.2的依赖树是:

org.springframework.boot:spring-boot-starter-web:jar:2.2.2.RELEASE:compile
+- org.springframework.boot:spring-boot-starter:jar:2.2.2.RELEASE:compile
|  +- org.springframework.boot:spring-boot:jar:2.2.2.RELEASE:compile
|  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.2.2.RELEASE:compile
|  +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.2.RELEASE:compile
|  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
|  |  |  +- ch.qos.logback:logback-core:jar:1.2.3:compile
|  |  |  \- org.slf4j:slf4j-api:jar:1.7.29:compile
|  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.12.1:compile
|  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.12.1:compile
|  |  \- org.slf4j:jul-to-slf4j:jar:1.7.29:compile
|  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
|  +- org.springframework:spring-core:jar:5.2.2.RELEASE:compile
|  |  \- org.springframework:spring-jcl:jar:5.2.2.RELEASE:compile
|  \- org.yaml:snakeyaml:jar:1.25:runtime
+- org.springframework.boot:spring-boot-starter-json:jar:2.2.2.RELEASE:compile
|  +- com.fasterxml.jackson.core:jackson-databind:jar:2.10.1:compile
|  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.10.1:compile
|  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.10.1:compile
|  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.10.1:compile
|  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.10.1:compile
|  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.10.1:compile
+- org.springframework.boot:spring-boot-starter-tomcat:jar:2.2.2.RELEASE:compile
|  +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.29:compile
|  +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.29:compile
|  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.29:compile
+- org.springframework.boot:spring-boot-starter-validation:jar:2.2.2.RELEASE:compile
|  +- jakarta.validation:jakarta.validation-api:jar:2.0.1:compile
|  \- org.hibernate.validator:hibernate-validator:jar:6.0.18.Final:compile
|     +- org.jboss.logging:jboss-logging:jar:3.4.1.Final:compile
|     \- com.fasterxml:classmate:jar:1.5.1:compile
+- org.springframework:spring-web:jar:5.2.2.RELEASE:compile
|  \- org.springframework:spring-beans:jar:5.2.2.RELEASE:compile
\- org.springframework:spring-webmvc:jar:5.2.2.RELEASE:compile
   +- org.springframework:spring-aop:jar:5.2.2.RELEASE:compile
   +- org.springframework:spring-context:jar:5.2.2.RELEASE:compile
   \- org.springframework:spring-expression:jar:5.2.2.RELEASE:compile

但正如 cmets 中所述,您应该尝试使用构建工具 (Maven/Gradle) 和公司范围的存储库管理器 (Nexus/Artifactory)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-23
    • 2016-09-20
    • 2017-10-09
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多