zoukankan      html  css  js  c++  java
  • SpringBoot入门系列:第一篇 Hello World

    跟随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存于myFirstProjectsrcmainjavacomexamplemyFirstProject下

    [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,存于myFirstProjectsrcmainjavacomexamplemyFirstProject下

    [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搭建,参考

  • 相关阅读:
    算法导论学习 之 插入排序
    python 的模块导入
    Python 模块的发布与上传
    Python 自学笔记《1》
    linux内核可以接受的参数 | Linux kernel启动参数 | 通过grub给内核传递参数
    Linux系统安装时分区的选择(推荐)
    Oracle 11G在用EXP 导出时,空表不能导出解决
    android TextView属性详解
    android中dip、dp、px、sp和屏幕密度
    android ImageView scaleType属性
  • 原文地址:https://www.cnblogs.com/Jeremy2001/p/6108500.html
Copyright © 2011-2022 走看看