博客
关于我
《springboot学习笔记 01》--打印helloworld
阅读量:231 次
发布时间:2019-02-28

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

Spring Boot项目基础搭建指南

一、项目基础搭建

  • 新建Spring Boot Maven项目

    创建一个普通的Maven项目,路径为C:\work\workspace\a-springboot-test

  • 配置POM文件

    在项目根目录下找到pom.xml文件,添加必要的Spring Boot依赖。以下是推荐的配置:

    org.springframework.boot
    spring-boot-starter-parent
    1.4.4.RELEASE
    org.springframework.boot
    spring-boot-starter-web
  • 创建测试类并运行

    新建一个测试类Demo.java,内容如下:

    import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Demo {    public static void main(String[] args) {        SpringApplication.run(Demo.class, args);    }}
  • 运行此类,启动Spring Boot应用,无需处理端口冲突即可正常运行。

    二、通过浏览器访问应用

  • 配置Spring MVC组件扫描

    src/main/resources/app.xml中添加以下配置:

  • 配置Web.xml文件

    src/main/resources/web.xml中添加以下配置:

    springboot-test
    org.springframework.web.servlet.DispatcherServlet
    contextConfigLocation
    classpath:app.xml
    springboot-test
    /
  • 编写Hello World控制器

    创建HelloWorldController类,内容如下:

    @RestController@RequestMapping("/api")public class HelloWorldController {    @GetMapping("/hello")    public String sayHello() {        return "Hello World!";    }}
  • 启动测试并验证

    使用IDE运行Demo.java,访问http://localhost:8080/api/hello,查看是否返回"Hello World!"。

  • 注意事项

    • 如果默认端口8080已占用,可以通过修改application.properties文件设置端口:

      server.port=8081
    • 配置完成后,项目即可通过浏览器访问,测试功能正常运行即可确认配置成功。

    通过以上步骤,您可以成功搭建一个Spring Boot项目并进行基本的应用开发。如果需要进一步优化或扩展,请参考Spring Boot官方文档或相关技术博客。

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

    你可能感兴趣的文章
    python | jsonschema,一个实用的 验证 JSON 数据结构 Python 库!
    查看>>
    python | lxml,一个超酷的 关于XML/HTML 文档 Python 库!
    查看>>
    python | mplfinance,一个有趣的金融数据可视化 Python 库!
    查看>>
    python | nipy,一个强大的关于 神经影像数据分析 的Python 库!
    查看>>
    python | pendulum,一个有趣的 日期和时间 Python 库!
    查看>>
    python | pluginbase,一个神奇的 关于插件框架 的Python 库!
    查看>>
    python | pyparsing,一个强大的 Python 库!
    查看>>
    python | pyqtgraph,一个神奇的 Python 库!
    查看>>
    python读取文本文件数据
    查看>>
    python | Python mock对象与测试替身
    查看>>
    python | Python pandas实现数据追加和合并的最佳方法
    查看>>
    python | Python 中检查一个数字是否是三态数
    查看>>
    python | Python 蒙特卡洛模拟
    查看>>
    python | Python中的类多态:方法重写和动态绑定
    查看>>
    python | Python模块缓存:sys.modules机制
    查看>>
    python | pyupgrade,一个有趣的 Python 库!
    查看>>
    Python | qutip,一个高级的 Python 库!
    查看>>
    python | reflex,一个无敌的 Python 库!
    查看>>
    python | reloading,一个强大的 Python 库!
    查看>>
    python | rich,一个无敌的 Python 库!
    查看>>