SpringBoot入门系列:第一篇 Hello World

来源:互联网 发布:淘宝2015版本下载安装 编辑:程序博客网 时间:2024/05/21 09:52

一、准备工作

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
print?
  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>  
<?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>com.example</groupId>    <artifactId>myFirstproject</artifactId>    <version>0.0.1-SNAPSHOT</version>    <!-- Inherit defaults from Spring Boot -->    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.4.0.BUILD-SNAPSHOT</version>    </parent>    <!-- Add typical dependencies for a web application -->    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>    </dependencies>    <!-- Package as an executable jar -->    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build>    <!-- Add Spring repositories -->    <!-- (you don't need this if you are using a .RELEASE version) -->    <repositories>        <repository>            <id>spring-snapshots</id>            <url>http://repo.spring.io/snapshot</url>            <snapshots><enabled>true</enabled></snapshots>        </repository>        <repository>            <id>spring-milestones</id>            <url>http://repo.spring.io/milestone</url>        </repository>    </repositories>    <pluginRepositories>        <pluginRepository>            <id>spring-snapshots</id>            <url>http://repo.spring.io/snapshot</url>        </pluginRepository>        <pluginRepository>            <id>spring-milestones</id>            <url>http://repo.spring.io/milestone</url>        </pluginRepository>    </pluginRepositories></project>
文档内容从SpringBoot的文档拷贝

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

[java] view plain copy
print?
  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. }  
package com.example.myFirstProject;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}
5、编制Example.java,存于myFirstProject\src\main\java\com\example\myFirstProject下

[java] view plain copy
print?
  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. }  
package com.example.myFirstProject;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@EnableAutoConfigurationpublic class Example {    @RequestMapping("/")    String home() {        return "Hello World!";    }    @RequestMapping("/hello/{myName}")    String index(@PathVariable String myName) {        return "Hello "+myName+"!!!";    }}
二、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私服、手动更新索引





                </div>