【发布时间】: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 下载,他们可以安装公司存储库,例如 Nexus 或 Artifactory。
-
您确实需要 Maven 或 Gradle 之类的东西来使用 Spring Boot。它创建的 jar/war 具有特殊的包装和专门的启动方式。如果没有 Maven 或 Gradle,您需要确保自己也复制它,否则交付的工件将无法工作。
标签: java spring-boot maven tomcat microservices