SpringBoot入门系列:第一篇 Hello World

出于对作者的尊重和感谢,原文地址为 http://blog.csdn.net/lxhjh/article/details/51711148


   跟随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
  在CODE上查看代码片 派生到我的代码片
  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
  在CODE上查看代码片 派生到我的代码片
  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
  在CODE上查看代码片 派生到我的代码片
  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搭建,参考

   

Maven入门指南⑤:使用Nexus搭建Maven私服


使用nexus搭建maven私服、手动更新索引



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值