A, environmental

  • Java 8
  • Maven 3.3 +
  • Idea 2018 (Personal)

The environment has to be installed first.

About Maven Configuration

Configuring a Local Warehouse

Download the zip package from the official website, unzip it, and create a repository directory.

Open the configuration file settings. XML in the conf directory.

At line 55, configure your repository path.

Configure Ali Cloud mirroring

Continue to configure ali cloud mirror in the configuration file.

<mirrors> <mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> </mirrors> <profiles> <profile> < ID > JDK-1.8 </ ID > <activation> <activeByDefault>true</activeByDefault> < JDK >1.8</ JDK > </activation> <properties> Piler < maven.com. Source > 1.8 < / maven.com piler. Source > < maven.com piler. Target > 1.8 < / maven.com piler. Target > The < maven.compiler.com pilerVersion > 1.8 < / maven.compiler.com pilerVersion > < / properties > < / profile > < / profiles >Copy the code

Profiles here specifies that JDK 1.8 is used for compilation in case something else goes wrong during development. Find an empty position to paste.

Write HelloWorld

1. Idea Set Maven

Open the Settings of idea and set up maven locally.

2. Create a Maven project

The next step:

Click Finish to finish.

3. Import the parent project to the POM

Open the POM.xml import.

<! GroupId >org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> < version > 2.3.4. RELEASE < / version > < / parent >Copy the code

4. Add dependencies

It used to take a lot of work to develop a Web. Now it’s just a matter of adding a dependency. Again in POM.xml:

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>  </dependency> </dependencies>Copy the code

Once the import is complete, click on the library here and you’ll see that it has everything you need.

5. Write code

(1) Main program class

package com.pingguo.boot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; // Mark this as a SpringBoot application, this class is the main program class, @springBootApplication public class MainApplication {public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); }}Copy the code

Then, as before, write a controller:

package com.pingguo.boot.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String Hello() { return "Hello SpringBoot2"; }}Copy the code
  • @RestControllerIs a composite annotation that represents@Controller + @ResponseBody. This knowledge is already aheadspringMVCI’ve studied it in related content.

6. Run the program

Directly run the main run program written above.

Click Run:

It’s that simple.

7. Access request

Open a browser to http://localhost:8080/hello:

The request succeeded.

8. Simplify the configuration

Springboot can simplify configuration as much as possible by, for example, extracting all future configurations into a configuration file called application.properties.

Here, you can change some Settings about Tomcat, springMVC related Settings, etc., such as changing the Tomcat port number:

server.port=8888
Copy the code

Restarting the application:

IO /spring-boot…

The arrangements are clear.

9. Simplify deployment

To deploy the application before, you had to break the application into a WAR package.

Now, simply by introducing a dependency, you can put the project directly into an executable JAR package that contains the runtime environment, so it can be run directly on the target server.

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>  </plugin> </plugins> </build>Copy the code

Click Package in Maven on the right.

Packing is done.

Open the target directory tree on the left and you already have the JAR package.

Now stop the service you just ran and try running the jar directly in Java:

Java - jar boot - 01 - the helloworld - 1.0 - the SNAPSHOT. The jarCopy the code

The startup succeeded.