springboot第一个helloworld

本文介绍了如何使用Spring Boot创建第一个 HelloWorld 应用。从配置Eclipse环境、生成Spring Boot工程,到编写Controller,最后通过Maven构建并运行jar包进行测试,详细步骤助你快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、springboot入门
1.配置eclipse环境
Jdk1.8、maven
2.生成springboot工程demo
进入https://start.spring.io/,选择相应选项后,点generate生成
在这里插入图片描述
3.在eclipse引入demo工程
按照maven工程引入
4.查看主程序

package com.popz.popz01;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/*
 * 工程由https://start.spring.io/生成
 */

@SpringBootApplication//标注一个主程序类,说明这是一个springboot应用
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

5.编写controller

package com.popz.popz01.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
	
	@ResponseBody
	@RequestMapping("/hello")
	public String Hello() {
		
		return "Hello World!";
	}

}

6.运行主程序测试

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

2020-05-31 19:53:35.416  INFO 4992 --- [           main] com.popz.popz01.Application              : Starting Application on HeatoN-PC with PID 4992 (D:\myproject\popz-0.01\popz-0.01\target\classes started by HeatoN in D:\myproject\popz-0.01\popz-0.01)
2020-05-31 19:53:35.421  INFO 4992 --- [           main] com.popz.popz01.Application              : No active profile set, falling back to default profiles: default
2020-05-31 19:53:38.609  INFO 4992 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-05-31 19:53:38.627  INFO 4992 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-05-31 19:53:38.627  INFO 4992 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.35]
2020-05-31 19:53:38.763  INFO 4992 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-05-31 19:53:38.763  INFO 4992 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3247 ms
2020-05-31 19:53:38.989  INFO 4992 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-05-31 19:53:39.232  INFO 4992 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-31 19:53:39.246  INFO 4992 --- [           main] com.popz.popz01.Application              : Started Application in 12.415 seconds (JVM running for 13.091)
2020-05-31 19:56:17.689  INFO 4992 --- [nio-8080-exec-3] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-05-31 19:56:17.689  INFO 4992 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-05-31 19:56:17.702  INFO 4992 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Completed initialization in 13 ms
7.地址栏输入http://localhost:8080/hello
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200531204842363.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjA1ODM1OQ==,size_16,color_FFFFFF,t_70)

8.简化部署

<!-- 这个插件,可以将应用打成一个可执行jar包 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

##右击项目,选择Run As - Maven clean
##右击项目,选择Run As - Maven install
##成功后 会在项目的target文件夹下生成jar包

[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ popz-0.01 ---
[INFO] Downloading from : https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.pom
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.pom (998 B at 2.4 kB/s)
[INFO] Downloading from : https://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.pom
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.pom (11 kB at 28 kB/s)
[INFO] Downloading from : https://repo.maven.apache.org/maven2/org/apache/commons/commons-parent/22/commons-parent-22.pom
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/org/apache/commons/commons-parent/22/commons-parent-22.pom (42 kB at 104 kB/s)
[INFO] Downloading from : https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.pom
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.pom (4.0 kB at 10 kB/s)
[INFO] Downloading from : https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.jar
[INFO] Downloading from : https://repo.maven.apache.org/maven2/classworlds/classworlds/1.1-alpha-2/classworlds-1.1-alpha-2.jar
[INFO] Downloading from : https://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.jar
[INFO] Downloading from : https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.jar
[INFO] Downloading from : https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.jar
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.jar (121 kB at 146 kB/s)
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/classworlds/classworlds/1.1-alpha-2/classworlds-1.1-alpha-2.jar (38 kB at 45 kB/s)
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.jar (239 kB at 240 kB/s)
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.jar (155 kB at 156 kB/s)
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.jar (233 kB at 196 kB/s)
[INFO] Installing D:\myproject\popz-0.01\popz-0.01\target\popz-0.01-0.0.1-SNAPSHOT.jar to C:\Users\HeatoN\.m2\repository\com\popz\popz-0.01\0.0.1-SNAPSHOT\popz-0.01-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\myproject\popz-0.01\popz-0.01\pom.xml to C:\Users\HeatoN\.m2\repository\com\popz\popz-0.01\0.0.1-SNAPSHOT\popz-0.01-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:08 min
[INFO] Finished at: 2020-05-31T20:58:08+08:00
[INFO] ------------------------------------------------------------------------

在cmd中运行java -jar xxx.jar即可启动应用
http://localhost:8080/hello
可验证

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值