SpringBoot入门系列:Hello World

来源:互联网 发布:kinect手势识别算法 编辑:程序博客网 时间:2024/05/22 16:58

跟随SpringBoot的文档(http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-documentation)学习,前后几章关联才调通代码,煞是痛苦,在这里记录结果,过程隐忍。

一、准备工作

1、根据Maven工程特点,建立文档结果

myFirstProject

  +-src

    +-main

      +-Java

      +-resources

    +-test

      +-java

      +-resources

2、再在src/main/java下依次建立文件夹com,example,myFirstProject,可以构成Maven工程包(package)-->com.example.myFirstProject,最后文档结构如图1

                           图1

3、编制pom.xml,存于myFirstProject文件夹下,与src同级

[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>com.example</groupId>  
  7.     <artifactId>myFirstproject</artifactId>  
  8.     <version>0.0.1-SNAPSHOT</version>  
  9.   
  10.     <!-- Inherit defaults from Spring Boot -->  
  11.     <parent>  
  12.         <groupId>org.springframework.boot</groupId>  
  13.         <artifactId>spring-boot-starter-parent</artifactId>  
  14.         <version>1.4.0.BUILD-SNAPSHOT</version>  
  15.     </parent>  
  16.   
  17.     <!-- Add typical dependencies for a web application -->  
  18.     <dependencies>  
  19.         <dependency>  
  20.             <groupId>org.springframework.boot</groupId>  
  21.             <artifactId>spring-boot-starter-web</artifactId>  
  22.         </dependency>  
  23.     </dependencies>  
  24.   
  25.     <!-- Package as an executable jar -->  
  26.     <build>  
  27.         <plugins>  
  28.             <plugin>  
  29.                 <groupId>org.springframework.boot</groupId>  
  30.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  31.             </plugin>  
  32.         </plugins>  
  33.     </build>  
  34.   
  35.     <!-- Add Spring repositories -->  
  36.     <!-- (you don't need this if you are using a .RELEASE version) -->  
  37.     <repositories>  
  38.         <repository>  
  39.             <id>spring-snapshots</id>  
  40.             <url>http://repo.spring.io/snapshot</url>  
  41.             <snapshots><enabled>true</enabled></snapshots>  
  42.         </repository>  
  43.         <repository>  
  44.             <id>spring-milestones</id>  
  45.             <url>http://repo.spring.io/milestone</url>  
  46.         </repository>  
  47.     </repositories>  
  48.     <pluginRepositories>  
  49.         <pluginRepository>  
  50.             <id>spring-snapshots</id>  
  51.             <url>http://repo.spring.io/snapshot</url>  
  52.         </pluginRepository>  
  53.         <pluginRepository>  
  54.             <id>spring-milestones</id>  
  55.             <url>http://repo.spring.io/milestone</url>  
  56.         </pluginRepository>  
  57.     </pluginRepositories>  
  58. </project>  
文档内容从SpringBoot的文档拷贝

4、编制Application.java存于myFirstProject\src\main\java\com\example\myFirstProject下

[java] view plain copy
  1. package com.example.myFirstProject;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5.   
  6.   
  7. @SpringBootApplication  
  8. public class Application {  
  9.   
  10.     public static void main(String[] args) {  
  11.         SpringApplication.run(Application.class, args);  
  12.     }  
  13. }  
5、编制Example.java,存于myFirstProject\src\main\java\com\example\myFirstProject下

[java] view plain copy
  1. package com.example.myFirstProject;  
  2.   
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
  4. import org.springframework.web.bind.annotation.PathVariable;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6. import org.springframework.web.bind.annotation.RestController;  
  7.   
  8. @RestController  
  9. @EnableAutoConfiguration  
  10. public class Example {  
  11.       
  12.     @RequestMapping("/")  
  13.     String home() {  
  14.         return "Hello World!";  
  15.     }  
  16.       
  17.     @RequestMapping("/hello/{myName}")  
  18.     String index(@PathVariable String myName) {  
  19.         return "Hello "+myName+"!!!";  
  20.     }  
  21. }  
二、Maven工程导入

1、启动eclipse

    1.1、java-->jdk1.7.0_80x64

    1.2、maven-->Apache Maven 3.3.3

    1.3、Eclipse Java EE IDE for Web Developers. Version: Mars Release (4.5.0)

    1.4、为本练习,新建一个workgroup

2、在eclipse中,依次点击file-->import-->Maven-->Existing Maven Projects-->Next-->Browse-->定位到myFirstProject文件夹-->确定--Finish

3、导入结果如图2

图2

三、运行

1、在eclipse的工程myFirstProject上右击鼠标,选择Run as-->java Application,如图3

2、在select Java Aplication中选择“Application -com.example.myFirstProject”,如图4

图4

3、再次点击“OK”按钮,在Eclipse的Console中开始打印如图5


图5

4、打开浏览器,输入http://localhost:8080,显示如图6

5、在浏览器中,输入http://localhost:8080/hello/SpringBoot

四、后记

痛则不通,通则不痛。这个例子非常之简单,为了这个简单,费事不少。为了这个例子能够成功,最好做以下准备

1、构建本地的Maven伺服,否则速度痛苦

2、Sonatype Nexus尽量和jdk相对应的版本,不要最求最新,否则可能启动不起来。

3、Sonatype Nexus搭建,参考

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 腰椎间盘突出症的锻炼 腰椎间盘突出的锻炼 腰椎间盘突出的护理 腰椎间盘突出按摩手法 腰椎间盘突出和脱出 腰椎间盘突出如何保养 腰椎间盘突出的保养 腰椎间盘突出有何症状 腰椎间盘突出医院好 得了腰椎间盘突出怎么办 腰椎间盘突出的锻炼方法 腰椎间盘为什么会突出 腰椎间盘突出要注意哪些 腰椎间盘突出吃中药 腰椎间盘突出的腰带 腰椎间盘突出按摩器 腰椎间盘突出新疗法 腰椎间盘突出费用多少 腰椎间盘突出微创多少钱 腰椎间盘突出哪里医院好 腰椎间盘突出怎样按摩 腰椎间盘突出哪个医院好 腰椎间盘突出适合什么运动 腰椎间盘突出能自愈吗 腰椎间盘突出能复位吗 腰椎间盘突出手法复位 腰椎间盘突出能做瑜伽吗 霍华德腰椎间盘突出 腰椎间盘突出拍片能看出来吗 腰椎间盘突出去那家医院好 腰椎间盘突出能用按摩器吗 腰椎间盘突出专业医院 腰椎间盘突出应该看什么科 腰椎间盘突出要住院吗 腰椎间盘突出的微创疗法 腰椎间盘滑脱是怎么回事 腰椎间盘突出康复训练 腰椎间盘突出有什么好的办法 腰椎间盘突出 哪个医院 腰椎间盘突出中药方 腰椎间盘突出能游泳吗