从0开始搭建SpringBoot

上篇文章介绍了springBoot的各类优势,嗯,它很容易就能搭建一个web应用,那么具体怎么作呢?java

那么咱们简单的搭建一个hello 的web应用,这应用很是简单,写一个controller,而后让这controller能在浏览器里面访问就ok了。web

你可使用eclipse,也可使用idea或其余任何你习惯使用的开发工具,这里我使用ideaspring

新建一个maven应用

完成后把里面的pom文件修改为apache

<?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>toroot</groupId>
    <artifactId>spingboot01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>
浏览器

 

这个配置其实很简单,固然也能够解释一下tomcat

固然,你能够直接照着这些代码来完成这简单的功能,我并不建议你如今就来考虑为何必定要这么配置,纠结每个配置的意思,是运行,有个直观的感觉再说。架构

固然,你也能够看下这配置具体哪来的,后面有空再来看下为何这么配置。mvc

新建一个controller

新建一个controller,随便起个名字都行,这里就叫TorootController吧,新增一个sayhello的方法,并把相关的springmvc注解给加上app

package cn.toroot; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /**  * Created by www.Toroot.cn  * java资料学习交流群:523916260  */ @RestController @RequestMapping("/toroot") public class TorootController {     @RequestMapping("/hello")     public Object sayHello() {         return "hello toroot";     } }

新建一个启动类

接下来再增长一个启动类,类名无所谓,咱们这里就叫作TorootApp吧,其中具体的代码以下eclipse

package cn.toroot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /**  * Created by www.Toroot.cn  * java资料学习交流群:523916260  */ @SpringBootApplication public class TorootApp {     public static void main(String[] args) {         SpringApplication.run(TorootApp.class,args);         } }

测试运行

嗯,main方法有了,那么直接点运行试试效果呗,运行main方法后,看到。

嗯,很是好,虽然不知道为何但这webapp已经运行再tomcat里面了,经过浏览器运行下试试。

http://localhost:8080/toroot/hello

是否是感受很是容易,固然这案例也是经过官网改动而成

具体官网案例以下图所示

官网:http://projects.spring.io/spring-boot/

好了,至于具体的更高级的应用咱们留下次再分享吧,感兴趣的同窗能够java架构师学习交流群 862039307