从零开始学spring-boot(1)-Hello spring boot!

第一次看到springboot的时候就被它惊艳到了,相信开发java web的都知道,我们想要实现访问浏览器,返回“Hello word”需要付出多少努力,不管是SSH还是springMVC,想要实现“Hello word”,繁杂的XML配置会让你抓耳挠腮,而springboot的出现解放了我们的配置,让我们可以快速的投身到业务中去。当然减少的配置只是springboot的一个特性,更多的特性请移步springboot官方指南仔细研究

我将使用IDEA作为开发环境,Maven作为项目构建工具来学习springboot。接下来让我们开始Hello word!

1.首先创建一个Maven的空项目。


2.打开pom.xml文件将以下代码复制到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>spring-boot-learning-one</groupId>
    <artifactId>spring-boot-learning-one</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <!-- Additional lines to be added here... -->

    <!-- (you don't need this if you are using a .RELEASE version) -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
</project>
3.为了保持代码结构的整洁性,我们将Controller和Application分开编写



4.在Application中添加以下代码

package com.zity.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by Andy on 2016/10/26.
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
5.在Controller中添加以下代码
package com.zity.springboot.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Andy on 2016/10/26.
 */
@RestController
@RequestMapping(value = "/")
public class Controller {
    @RequestMapping(value = "/hello")
    public String sayHello(){
        return "Hello word!";
    }
}

6.此时所有的搭建工作就已经完毕,没有任何一个xml配置,如果你仔细观察会注意到Application中有main方法,没错,springboot可以通过main方法来启动,由于它可以内置tomcat(或者jetty),所以它并不需要另外配置tomcat来运行。此时,只需要在main方法上左键在控制台会输出spring的字样,当出现


说明你的程序已经成功运行起来了。

7.然后在浏览器访问http://127.0.0.1:8080/hello,当看到“Hello word!”时说明你的第一个springboot程序已经成功编写完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值