Spring Boot (1) Hello World

Begin at Hello World

This section describes how to develop a simple “Hello World!” web application that highlights some of Spring Boot’s key features. We use Maven to build this project, since most IDEs support it.

1. Creating the POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.my.study.springboot</groupId>
	<artifactId>hello-world</artifactId>
	<version>0.0.1-RELEASE</version>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.14.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

2. Writing the Code

package com.my.study.springboot.helloworld;

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

@SpringBootApplication
public class Main {
	public static void main(String[] args) {
		SpringApplication.run(Main.class, args);
	}
}
package com.my.study.springboot.helloworld;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	@RequestMapping("/")
	public String helloworld() {
		return "hello world";
	}
}

3. Code Structure


4. Run The Hello World

At this point, your application should work. Since you used the spring-boot-starter-parent POM, you have a useful run goal that you can use to start the application. Type mvn spring-boot:run from the root project directory to start the application. You should see output similar to the following:

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

2018-07-04 11:59:20.202  INFO 12356 --- [           main] com.my.study.springboot.helloworld.Main  : Starting Main on CNENWANGS56L1C with PID 12356 ...
2018-07-04 11:59:20.268  INFO 12356 --- [           main] com.my.study.springboot.helloworld.Main  : No active profile set, falling back to default profiles: default
2018-07-04 11:59:20.485  INFO 12356 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@571349e4: startup date [Wed Jul 04 11:59:20 CST 2018]; root of context hierarchy
2018-07-04 11:59:22.684  INFO 12356 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
...
...

If you open a web browser to localhost:8080, you should see the following output:

hello world

To gracefully exit the application, press ctrl-c.

You can also build a jar file by executing maven command mvn clean package, and then run a single jar file,

C:\D\work\projects\sts\springboot-helloworld>java -jar target/hello-world-0.0.1-RELEASE.jar

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

2018-07-04 12:04:12.179  INFO 14168 --- [           main] com.my.study.springboot.helloworld.Main  : Starting Main v0.0.1-RELEASE on CNENWANGS56L1C with PID 14168 ...
2018-07-04 12:04:12.200  INFO 14168 --- [           main] com.my.study.springboot.helloworld.Main  : No active profile set, falling back to default profiles: default
2018-07-04 12:04:12.405  INFO 14168 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@69663380: startup date [Wed Jul 04 12:04:12 CST 2018]; root of context hierarchy
As before, to exit the application, press  ctrl-c .

猜你喜欢

转载自blog.csdn.net/funnyrand/article/details/80910152