SpringBoot学习 快速入门篇

1,前言:java

这篇学习SpringBoot的入门文章是我在Spring官网(http://spring.io/guides/gs/rest-service/)上学习以后的及时梳理、总结,以期巩固本身所学的、加深本身对它的理解,但愿对你们也能有所帮助。web


2,整体步骤:spring

总的来讲,建立一个入门式的SpringBoot项目很是简单,只须要:
apache

2.1,在eclipse中建立一个maven项目(我是使用eclipse建立这个工程)。json

2.2,在pom.xml文件中导入运行SpringBoot所须要的依赖配置。浏览器

2.3,建立一个包含main方法的类,并使用@SpringBootApplication注解该类。springboot

2.4,在控制层建立一个使用@RestController注解的Controller类,并简单的实现一个rest请求方法。app


3,具体步骤:eclipse

3.1,在eclipse中建立maven项目,以下图:maven



点击finish等待完成项目的建立。

而后右键项目,找到Properties,点击Java Build Path,点击Libraries,将J2SE-1.5移除,点击Add Libarary加上本身本地安装的jdk8,点击finish,点击OK。




这样就建立了一个maven项目了。

3.2,向pom.xml中添加SpringBoot运行须要的依赖配置:

将下列代码替换pom.xml文件中配置,

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

    <groupId>com.xlat.springboot</groupId>
    <artifactId>greeting</artifactId>
    <version>1.0.0</version>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


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

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>

此时项目上面有一个小红叉,这时根据Problems视图中的错误提示,咱们还要更新pom.xml最新配置,

右键项目,选择 Maven - 点击Update Project


3.3,建立一个Application类,用于启动SpringBoot项目:

该类代码以下,

package com.xlat.springboot;

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

@SpringBootApplication
public class Application {

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

3.4,在控制层建立一个GreetingController类、在实体层建立一个Greeting类:

GreetingController类代码以下,

package com.xlat.springboot.controller;

import java.util.concurrent.atomic.AtomicLong;

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

import com.xlat.springboot.entity.Greeting;

@RestController
public class GreetingController {
	private static final String template = "Hello, %s!";
	private final AtomicLong counter = new AtomicLong();
	
	@RequestMapping("/greeting")
	public Greeting greeting(@RequestParam(value = "name", defaultValue = "SpringBoot") String name) {
		return new Greeting(counter.incrementAndGet(), String.format(template, name));
	}

}

Greeting类的代码以下,

package com.xlat.springboot.entity;

public class Greeting {
	private final long id;
	private final String content;
	
	public Greeting(long id, String content) {
		this.id = id;
		this.content = content;
	}
	
	public long getId() {
		return id;
	}
	public String getContent() {
		return content;
	}
}

完成了以上步骤后启动项目:运行Application类的main方法,运行成功后,这时就能够访问项目了,

在浏览器地址栏输入:localhost:8080/greeting/,浏览器返回如下页面:


在浏览器地址栏输入:localhost:8080/greeting?name=world,浏览器返回如下页面:


经过以上的这些操做,咱们就完成了一个简单、入门级的SpringBoot建立过程。


4,相关解释:

在GreetingController类的greeting方法中并无指定请求方式,此时默认的是GET请求方式。

@SpringBootApplication注解的做用至关于@Configuration、@EnableAutoConfiguration、@ComponentScan这三个注解修饰类时所起的做用。

@RestController注解的做用至关于@ResponseBody、@Controller这两个注解修饰类时所起的做用。


5,总结:

OK啦,这篇文章写完了,虽然这篇文章看起来很基础、很简单,可是在学习技术的道路上,基础是必不可少的,毕竟不积硅步无以致千里嘛。我是个写博客的新人,这是我写的第二篇csdn博客,但愿你们多多给我提些建议,三人行必有我师,只有互相交流、互相学习咱们才能进步的更快!大笑