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中,在这里需要提醒一下,第一次创建需要导入相关依赖,需要挺长时间,请耐心等待。

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  4.     <modelVersion>4.0.0</modelVersion>  
  5.   
  6.     <groupId>spring-boot-learning-one</groupId>  
  7.     <artifactId>spring-boot-learning-one</artifactId>  
  8.     <version>0.0.1-SNAPSHOT</version>  
  9.   
  10.     <parent>  
  11.         <groupId>org.springframework.boot</groupId>  
  12.         <artifactId>spring-boot-starter-parent</artifactId>  
  13.         <version>2.0.0.BUILD-SNAPSHOT</version>  
  14.     </parent>  
  15.     <dependencies>  
  16.         <dependency>  
  17.             <groupId>org.springframework.boot</groupId>  
  18.             <artifactId>spring-boot-starter-web</artifactId>  
  19.         </dependency>  
  20.     </dependencies>  
  21.     <!-- Additional lines to be added here... -->  
  22.   
  23.     <!-- (you don't need this if you are using a .RELEASE version) -->  
  24.     <repositories>  
  25.         <repository>  
  26.             <id>spring-snapshots</id>  
  27.             <url>http://repo.spring.io/snapshot</url>  
  28.             <snapshots><enabled>true</enabled></snapshots>  
  29.         </repository>  
  30.         <repository>  
  31.             <id>spring-milestones</id>  
  32.             <url>http://repo.spring.io/milestone</url>  
  33.         </repository>  
  34.     </repositories>  
  35.     <pluginRepositories>  
  36.         <pluginRepository>  
  37.             <id>spring-snapshots</id>  
  38.             <url>http://repo.spring.io/snapshot</url>  
  39.         </pluginRepository>  
  40.         <pluginRepository>  
  41.             <id>spring-milestones</id>  
  42.             <url>http://repo.spring.io/milestone</url>  
  43.         </pluginRepository>  
  44.     </pluginRepositories>  
  45. </project>  
3.为了保持代码结构的整洁性,我们将Controller和Application分开编写



4.在Application中添加以下代码

[java]  view plain  copy
  1. package com.zity.springboot;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5.   
  6. /** 
  7.  * Created by Andy on 2016/10/26. 
  8.  */  
  9. @SpringBootApplication  
  10. public class Application {  
  11.     public static void main(String[] args) {  
  12.         SpringApplication.run(Application.class, args);  
  13.     }  
  14. }  
5.在Controller中添加以下代码
[java]  view plain  copy
  1. package com.zity.springboot.web;  
  2.   
  3. import org.springframework.web.bind.annotation.RequestMapping;  
  4. import org.springframework.web.bind.annotation.RestController;  
  5.   
  6. /** 
  7.  * Created by Andy on 2016/10/26. 
  8.  */  
  9. @RestController  
  10. @RequestMapping(value = "/")  
  11. public class Controller {  
  12.     @RequestMapping(value = "/hello")  
  13.     public String sayHello(){  
  14.         return "Hello word!";  
  15.     }  
  16. }  

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


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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值