Integrated SpringMVC for SpringBoot initial experience

Keywords: Java SpringBoot Spring Maven xml

As a developer, we all know that SpringBoot is based on Spring 4.0, which not only inherits the original excellent features of the Spring framework, but also further simplifies the entire process of building and developing Spring applications by simplifying the configuration.In addition, SpringBoot integrates a number of frameworks to resolve issues such as package-dependent version conflicts and referential instability.

Features of SpringBoot:

* Provide a faster Start experience for Spring-based development
* Out of the box, no code generation, no XML configuration required.Defaults can also be modified to meet specific needs
* Provides some common non-functional features in large projects, such as embedded servers, security, metrics, health testing, external configuration, etc.
SpringBoot is not an enhancement of Spring functionality, it provides a quick way to use Spring

Here's how SpringBoot integrates SpringMVC:

1. Create a Maven Project using IDEA

 

 

Fill in the relevant information about the project;

 

 

 

Click Finish and wait for the project to be created.

 

 

SpringBoot requires that projects depend on spring-boot-starter-parent to inherit the start of SpringBoot;

 <!--SpringBoot Start Dependency spring-boot-starter-parent-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

Integrate SpringMVC at the same time, to import web startup dependency;

 <dependencies>
        <!--web Startup Dependency-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

After importing coordinates, the pom.xml file is:

<?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>
    <!--SpringBoot Start Dependency spring-boot-starter-parent-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <groupId>com.xyfer</groupId>
    <artifactId>springbootDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--web Startup Dependency-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

Write a guide class for SpringBoot to start the SpringBoot project;

package com.xyfer;

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

@SpringBootApplication
public class MySpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class);
    }

}

Write code for the Controller layer;

package com.xyfer.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DemoController {
    @RequestMapping("/test")
    @ResponseBody
    public String test(){
        return "Hello SpringBoot!";
    };
}

At this point, a simple project for SpriingBoot has been built with the following project catalog structure;

 

 

 

The SpringBoot Default scan boot class MySpringBootApplication.java sibling packages and their subpackages, so the controller layer can be scanned;

 

Start boot class for testing;

 

 

When the console prints the following information, the SpringBoot project is started successfully;

 

 

Enter the address in the browser: http://localhost:8080/test, access succeeded!

 

 

This completes a simple SpringBoot project!

Posted by PHP_ColdFusion on Wed, 11 Sep 2019 09:58:59 -0700