springBoot入门实例

来源:互联网 发布:网络攻击的种类是什么 编辑:程序博客网 时间:2024/06/18 13:02

环境:idea+java8

1.新建maven工程

2.在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.zdy.test</groupId>    <artifactId>springBoot</artifactId>    <version>1.0-SNAPSHOT</version>    <name>springBoot</name>    <url>http://maven.apache.org</url>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.4.3.RELEASE</version>    </parent>    <!-- Add typical dependencies for a web application -->    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>3.8.1</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-thymeleaf</artifactId>        </dependency>    </dependencies>    <!-- Package as an executable jar -->    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>
3.在src/main/java下新建包com.zdy.springBoot,在此包下新建类Application.java
package com.springBoot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * Created by zengd on 2017/10/19. */
@SpringBootApplication@RestControllerpublic class Application {    @RequestMapping("/")    public String greeting() {        return "Hello World!";    }    public static void main(String[] args) {        // TODO Auto-generated method stub               SpringApplication.run(Application.class, args);          }}


4.启动main方法


启动成功。在浏览器中访问http://localhost:8080/,显示Hello World!

**注意,Application.java一定要放在包下,不可以直接放在src/main/java下,否则启动时会报错