Use Spring Boot to implement e-commerce system Web API (1) Hello World, spring e-commerce
Let's start with Hello World! Start.
I. Build tools
Assume that Java SDK 8 has been installed.
Install build tools
We chose gradle to build the project.
Https://gradle.org/install this web page will tell you how to install.
We can also choose to manually install, first download the package from the https://gradle.org/releases.
Mkdir spring-hello & cd spring-hello
Create a text file named build. gradle,
cd . > build.gradle
Create the directory src \ main \ java \ com \ hang and enter the Directory
mkdir src\main\java\com\hang && cd src\main\java\com\hang
Create the java source file App. Java in the src \ main \ java \ com \ hang directory.
cd . > App.java
Create the controller directory under the src \ main \ java \ com \ hang directory and go to the controller directory.
mkdir controller && cd controller
Create a source file named HelloController. java
cd . > HelloController.java
The directory structure is as follows:
Package com. hang; import org. springframework. boot. springApplication; import org. springframework. boot. autoconfigure. springBootApplication; @ SpringBootApplicationpublic class App {public static void main (String [] args) {SpringApplication. run (App. class, args );}}
Enter the following content in HelloController. java:
package com.hang.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController { @GetMapping("/") public String hello(){ return "Hello World!"; }}
Enter the following content in build. gradle:
buildscript { repositories { jcenter() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.4.RELEASE") }}apply plugin: 'java'apply plugin: 'org.springframework.boot'repositories { jcenter()}dependencies { compile 'org.springframework.boot:spring-boot-starter-web'}
Iv. Compilation
Compile the project and run the command in the spring-hello directory.
gradle build
This compilation process takes some time because Gradle needs to download the dependency library online.
If information appears, the compilation is successful.
Gradle bootRun
The console displays information such
Buildscript {repositories {jcenter ()} dependencies {classpath ("org. springframework. boot: spring-boot-gradle-plugin: 1.5.4.RELEASE ")}}
The above Code introduces the Gradle plug-in of Spring Boot, named "spring-boot-gradle-plugin". The version is "1.5.4.RELEASE", repositories {jcenter ()} tell Gradle To Go To The jcenter library to find this plug-in. The jcenter library is at https://bintray.com/bintray/jcenter, and the pipeline is slow. The apply in: 'org. springframework. boot' statement below takes effect only after the buildscript is added.
apply plugin: 'java'
Indicates that the Java Plug-in is used. We use Java to write this plug-in. The Java Plug-in is the built-in plug-in of Gradle, so you can use it directly.
apply plugin: 'org.springframework.boot'
Use the plug-in org. springframework. boot to build and run our Spring Boot program. Because this plug-in is not the built-in plug-in of Gradle, we should first introduce it in buildscript, as mentioned above. The above command "gradle bootRun" used to run the Spring Boot program also comes from this plug-in.
repositories { jcenter()}dependencies { compile 'org.springframework.boot:spring-boot-starter-web'}
The code above indicates that our program depends on the "spring-boot-starter-web" library, which needs to be downloaded from jcenter.
Now, the build. gradle file has been explained. Some may ask how to use "gradle bootRun" to run the program. You can execute it in the project directory.
gradle tasks
Gradle is task-based. This command lists the tasks supported in the current project.
SpringApplication. run (App. class, args );
This statement indicates that the Spring application is started directly.
The most important thing is the annotation "@ SpringBootApplication". Spring Boot uses annotations to implement complicated XML configurations before Sping, fully automated configuration. This annotation will automatically load the configuration. This annotation also contains an action to scan the sub-package Controller. It will automatically scan the sub-package and complete the configuration.
6.3 HelloController. java
@RestControllerpublic class HelloController { @GetMapping("/") public String hello(){ return "Hello World!"; }}
HelloController. java is simple, with only a few lines of code.
@ RestController indicates this Restful API,
@ GetMapper annotation indicates a Get request. If a Get request accesses the root directory, for example, input "http: // localhost: 8080/" in the browser to execute the hello () function, the function returns "Hello World! ". Similar requests include: @ PostMapper, @ PutMapper, and DeleteMapper, which correspond to the POST, PUT, and DELETE request methods of the HTTP protocol respectively.