SpringBoot入门系列:第二篇 再学Hello World

来源:互联网 发布:我欲封天时装进阶数据 编辑:程序博客网 时间:2024/05/21 10:56

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

Spring-Boot是Spring的新东东,为了让人尽快的使用,它提供了一个非常好的辅助工具,直接为我们生成Maven架构的工程。下面,我们通过helloworld看

一、在浏览器中打开http://start.spring.io/,如图

在Artifact中输入spring-boot-sample-helloworld,点击“Switch to the full version.”,勾选"web",然后点击“ Generate Project alt +”按钮,把文件保存到本地某个位置

二、下载文件导入eclips

1、解压下载的文件到某个文件夹;

2、在eclips中导入工程file->import->Import Existing Maven Projects-->Select Maven projects-->finish

3、在eclips中运行工程,正确则入图

三、添加HelloController

1、在包com.example中点击右键,选择new->class,再在Name中输入HelloController,入下图

在上图中点击"Finish"

2、在类上面条件声明@RestController

3、增加方法

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @RequestMapping("/")  
  2.     public String helloworld(){  
  3.         return "Hello world!";  
  4.     }  
  5.       
  6.     @RequestMapping("/hello/{name}")  
  7.     public String hellName(@PathVariable String Name){  
  8.         return "Hello "+Name;  
  9.     }  

4、保存Ctrl+S

5、自动添加引用 Ctrl+SHift+O

6、整个HelloController文件如下

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.example;  
  2.   
  3. import org.springframework.web.bind.annotation.PathVariable;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RestController;  
  6.   
  7. @RestController  
  8. public class HelloController {  
  9.   
  10.     @RequestMapping("/")  
  11.     public String helloworld(){  
  12.         return "Hello world!";  
  13.     }  
  14.       
  15.     @RequestMapping("/hello/{name}")  
  16.     public String hellName(@PathVariable String name){  
  17.         return "Hello "+name;  
  18.     }  
  19. }  
7、启动测试

在浏览器中依次输入

http://localhost:8080/

http://localhost:8080/hello/上帝

0 0
原创粉丝点击