博客
关于我
Spring boot入门应用
阅读量:178 次
发布时间:2019-02-28

本文共 1740 字,大约阅读时间需要 5 分钟。

环境要求

开发环境要求 JDK 1.8,项目管理工具为 Maven,开发工具为 Eclipse。

入门

1. 创建 Maven 项目

通过 Maven 创建项目,打开终端,执行以下命令:

mvn archetype:generate -DgroupId=cn.itsource -DartifactId=springboot-hello -Dversion=0.0.1-SNAPSHOT

2. 修改 pom 文件

打开项目的 pom.xml 文件,修改如下内容:

4.0.0
cn.itsource
springboot-hello
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.4.1.RELEASE
1.8
org.springframework.boot
spring-boot-starter-web

3. 项目错误修改方法

在开发过程中可能会遇到错误,以下是一些常见问题及解决方法:

  • Maven 项目无法启动:

    • 检查 pom.xml 文件,确保所有依赖版本正确。
    • 检查 JDK 版本是否为 1.8。
  • 代码编译错误:

    • 检查 imports 是否正确。
    • 确保项目根目录为 Maven 工程目录。
  • 启动类错误:

    • 检查主类是否注入了 @SpringBootApplication 注解。
    • 确保主类位于正确的包路径下。
  • 创建一个 controller

    HelloController.java

    package cn.itsource.springboot.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController {    @RequestMapping("/hello")    public String hello() {        return "Hello SpringBoot";    }}

    创建一个启动类

    App.java

    package cn.itsource.springboot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class App {    public static void main(String[] args) {        SpringApplication.run(App.class, args);    }}

    运行

  • 打开终端,进入项目根目录,执行:
  • mvn spring-boot:run
    1. 访问浏览器,地址为 http://localhost:8080/hello
    2. 验证

    3. 访问地址确认是否返回 "Hello SpringBoot"。

    4. 在浏览器中查看控制台输出,确保没有错误日志。

    5. 通过以上步骤,可以成功创建一个简单的 Spring Boot 项目,并验证其基本功能。

    转载地址:http://twrj.baihongyu.com/

    你可能感兴趣的文章
    poj 2965 The Pilots Brothers' refrigerator-1
    查看>>
    poj 3026( Borg Maze BFS + Prim)
    查看>>
    POJ 3041 - 最大二分匹配
    查看>>
    POJ 3041 Asteroids(二分匹配模板题)
    查看>>
    Qt笔记——标准文件对话框QFileDialog
    查看>>
    poj 3083 Children of the Candy Corn
    查看>>
    POJ 3083 Children of the Candy Corn 解题报告
    查看>>
    POJ 3253 Fence Repair C++ STL multiset 可解 (同51nod 1117 聪明的木匠)
    查看>>
    Qt笔记——控件总结
    查看>>
    poj 3262 Protecting the Flowers 贪心
    查看>>
    poj 3264(简单线段树)
    查看>>
    Qt笔记——布局管理三件套分割窗口、停靠窗口和堆栈窗口
    查看>>
    poj 3277 线段树
    查看>>
    POJ 3349 Snowflake Snow Snowflakes
    查看>>
    POJ 3411 DFS
    查看>>
    poj 3422 Kaka's Matrix Travels (费用流 + 拆点)
    查看>>
    Qt笔记——官方文档全局定义(二)Functions函数
    查看>>
    POJ 3468 A Simple Problem with Integers
    查看>>
    poj 3468 A Simple Problem with Integers 降维线段树
    查看>>
    poj 3468 A Simple Problem with Integers(线段树 插线问线)
    查看>>