Detailed explanation of springboot starter program

springboot program 

 

tool

 

 maven

 

 sts

 

 java8 (microservices)

 

 

1. First of all, springboot is a framework for quickly building spring (using java8 microservice technology). Compared with springmvc, it has less configuration because it has encapsulated springmvc. Therefore, we only need to introduce dependencies when using maven to develop Just

 

 

<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>

  <groupId>com.bldz.springboot</groupId>
  <artifactId>springboot-helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springboot-helloworld</name>
  <url>http://maven.apache.org</url>


  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <dependencies>
        <!-- Spring Boot web dependencies-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
		<dependency>
        	<groupId>junit</groupId>
        	<artifactId>junit</artifactId>
        </dependency>
    </dependencies>
</project>

 

 

 

 

2. Since the bottom layer of springboot uses springmvc, when we write the controller layer, we need to introduce the annotation @RestController, where @RestController is equivalent to @controller and @RequestBody of springmvc, the code is as follows

 

 

package org.spring.springboot;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @RestController is equivalent to springmvc's @controller and @RequestBody
 * @author qinxw
 *
 */
@RestController
public class HelloWorldController {
	@RequestMapping("/")
	public String hello() {
		return "hello";
	}
}

 

 

3. It is to write a springboot startup class, and use the @SpringBootApplication annotation of the springboot framework to mark this as the springboot startup class. The detailed code is as follows

 

 

package org.spring.springboot;

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

/**
 * springboot startup class
 * @author qinxianwei
 *
 */
@SpringBootApplication
public class App{
    public static void main(String[] args ){
        SpringApplication.run(App.class, args);
    }
}

 

 

4. Run the main method, the test results are as follows

 

 

 

 

 

 

 

 

 

Code cloud code download link 

 

     https://git.oschina.net/ymdrqq/springboot-helloworld.git

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326399431&siteId=291194637