spring-boot入门Demo

参考文档:

spring-boot官方文档,基于1.5.6.release版本。原版文档链接:
`https://docs.spring.io/spring-boot/docs/1.5.6.RELEASE/reference/htmlsingle/`;

1.pom.xml文件


<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.0modelVersion>
    <groupId>com.examplegroupId>
    <artifactId>myprojectartifactId>
    <version>0.0.1-SNAPSHOTversion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.2.RELEASEversion>
    parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>
    
project>

2.启动类

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

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

代码解释:
@RestController:标识该类为rest风格的controller;
@RequestMapping:接口的url映射;
以上两个都属于spring框架中的配置;
@EnableAutoConfiguration:让spring-boot自动配置项目,由于pom.xml文件中添加了spring-boot-starter-web,所以将会自动配置为一个web项目;

3.启动项目

运行main方法,项目启动后,在浏览器中输入`http://localhost:8080`,可返回`hello world`;

你可能感兴趣的:(spring-boot,入门,demo,spring-boot)