basic introduction
We use Maven to build and compile the code using SpringBoot 2.2.5.RELEASE version, and when we access through a browser link http: 8080 / hello, the outputs hello world on your browser: // localhost
Pom file
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mary.demo</groupId>
<artifactId>SpringBootDemo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringBootDemo</name>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Project structure
Project is structured as follows:
Item code
- Class inlet APP.java
package com.mary.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.lang.annotation.Inherited;
/**
*
*/
@EnableAutoConfiguration
@ComponentScan(basePackages={"com.mary.demo"})
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
``
2. HTTP API访问入口类HelloWorldController
```java
package com.mary.demo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@RequestMapping("/hello")
String home() {
return "Hello World!";
}
@RequestMapping("/test")
String test(){
return "test";
}
}
Start Service
When we finished writing the code, we can begin our project started, due SpringBoot default embedded Tomcat, so a direct start the main class APP.java file that is embedded tomcat will start
after the successful launch, you will see the following figure screen show, but will see the word started App in 1.668 seconds (JVM running for 3.484), it shows a successful start
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
2020-03-12 08:20:30.683 INFO 6752 --- [ main] com.mary.demo.App : Starting App on DESKTOP-FV5K1S8 with PID 6752 (E:\soft\code\SpringBootDemo\target\classes started by yangchao in E:\soft\code\SpringBootDemo)
2020-03-12 08:20:30.687 INFO 6752 --- [ main] com.mary.demo.App : No active profile set, falling back to default profiles: default
2020-03-12 08:20:31.609 INFO 6752 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-03-12 08:20:31.617 INFO 6752 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-03-12 08:20:31.617 INFO 6752 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-03-12 08:20:31.721 INFO 6752 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-03-12 08:20:31.721 INFO 6752 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 991 ms
2020-03-12 08:20:31.877 INFO 6752 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-12 08:20:32.013 INFO 6752 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
**2020-03-12 08:20:32.017 INFO 6752 --- [ main] com.mary.demo.App : Started App in 1.668 seconds (JVM running for 3.484)**
Through a browser to access the service
Open your browser, type http: // localhost: 8080 / hello, you will see what is shown in the following figure, shows that we succeeded to build the Hello World
Written in the last
This will build your success Hello World