introduction de base
Nous utilisons Maven pour construire et compiler le code en utilisant la version SpringBoot 2.2.5.RELEASE, et quand on accède par un lien de navigateur http: 8080 / bonjour, les sorties Bonjour tout le monde sur votre navigateur: // localhost
fichier Pom
<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>
Structure du projet
Le projet est structuré comme suit:
Code de l'article
- Classe entrée 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";
}
}
Démarrer le service
Quand nous avons fini d' écrire le code, nous pouvons commencer notre projet a commencé, en raison de défaut SpringBoot intégré Tomcat, donc un lien direct démarrer le fichier principal APP.java de classe qui est intégré tomcat commencera
après le lancement réussi, vous verrez la figure suivante Afficher l' écran, mais voir le mot commencé App en 1.668 secondes (machine virtuelle Java en cours d' exécution pour 3.484), il montre un bon départ
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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)**
Grâce à un navigateur pour accéder au service
Ouvrez votre navigateur, tapez http: // localhost: 8080 / bonjour, vous verrez ce qui est montré dans la figure ci-dessous, montre que nous avons réussi à construire le Bonjour tout le monde
Écrit dans la dernière
Cela va construire votre succès Bonjour tout le monde