【SpringBoot】从零开始之创建项目(一)

2019-03-28  本文已影响0人  欢子3824

前言

这个系列仅仅只是记录笔者学习SpringBoot过程中的心得,如有错误,欢迎指正。

准备

下载eclipse

选择Maven Project,记得勾选Use default Workspace location

image.png

填写详细信息

image.png
此时,项目结构为:
image.png
pom.xml添加SpringBoot相关的maven

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>

        <!-- Generic properties -->
        <java.version>1.7</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Spring -->
        <spring-framework.version>5.0.5.RELEASE</spring-framework.version>

    </properties>
    <dependencies>
        <!-- Spring and Transactions -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

src/main/resources下创建application.properties文件,这里是SpringBoot的配置文件,后续会有更多配置

server.port=8080
server.tomcat.uri-encoding=UTF-8

创建MyApplication,这里是程序的入口

@SpringBootApplication
public class MyApplication {

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

}
@RestController
@RequestMapping("/index")
public class IndexController {
    
    @GetMapping("/helloworld")
    public String index() {
        return "helloworld1";
    }
}

在MyApplication这个类中右键选择Run As# Spring Boot App,运行项目

image.png
在控制台看到如下输出,即表示运行成功

在浏览器输入http://localhost:8080/index/helloworld

image.png

你的认可,是我坚持更新博客的动力,如果觉得有用,就请点个赞,谢谢

上一篇 下一篇

猜你喜欢

热点阅读