springboot入门之hello world

来源:互联网 发布:微商作图软件 编辑:程序博客网 时间:2024/05/29 14:27

对于springboot的介绍我就不浪费口水了,新手入门也总结不出什么东西,主要是照网上找到的案例在这里重新写了一遍。

使用eclipse开发springboot项目,需要在eclipse安装spring插件,
Help->eclipse Marketplace->在popular中找到springtools(TST)选择install安装。
注:我这里还有一个错误,就是新建和导入不了springboot项目,提示Can not import using Maven because Can not use Maven: M2E (Eclipse Maven Tooling) is not installed,这是因为eclipse没有安装或maven插件版本过低,需要安装maven插件:Help->eclipse Marketplace->search查询maven找到Maven Integration for Eclipse(E2M)下载或更新。
本来打算使用sclipse开发的,但是在sclipse装完maven(e2m)插件和spring(TST)插件后死活创建不了springboot项目,只能安装 IntelliJ IDEA开发。

大体步骤:

1.new project 找到Spring Initializr ,选择jdk版本(1.7或以上),next填写数据,next选择依赖,这样只勾选web一个做测试用,finish。
2.删除 .mvn、mvnw、mvnw.cmd (简洁目录,可跳过)
3.编写demoController类(要在DemoApplication目录或下级目录)
4.右键DemoApplication选择run启动项目

TestController:

@RestControllerpublic class TestController {    @RequestMapping("test")    public String test(){        return "hello world";    }}

url访问:localhost:8080/test,可以看到页面显示hello world

@Controller和@RestController区别
1.都是表示是http请求入口
2.@RestController可以看作@Controller和@Responsbody结合,返回值可以是json对象

——

@RequestMapping说明
写在controller类上或方法上
value属性:对外提供的url
method属性:请求的方式如RequestMethod.GET和RequestMethod.POST

springboot项目结构大体介绍

新建的springboot项目目录结构很简单,需要注意的也就几个文件

1.pom.xml管理项目坐标

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

spring-boot-starter-parent代表springboot项目的顶级父类坐标,提供了相关maven默认依赖。这个坐标会在创建springboot项目的时候就默认添加上去。

2.main/java/项目路径/Application.java

这是springboot项目的启动类,程序的入口就是这里。在创建springboot项目后会自动创建好,可以右键这个类直接启动springboot项目。
项目如果新建controller类需要新建在这个Application类同级目录或之下目录

3.main/resources/application.properties

这是新建springboot项目的默认配置文件,也可以是application.yml格式,项目启动后会自动加载,在这里提供自定义配置属性也可以设置项目相关配置如服务端口号、日志等。
项目的资源文件一般也是放在resource目录下,一般会新建static和template等目录存放相关资源文件

下面附上大神的springboot博客:http://tengj.top/2017/04/24/springboot0/

原创粉丝点击