您的位置:首页 > 编程语言 > Java开发

SpringBoot入门系列: 再学Hello World(二)

2017-03-14 14:04 549 查看
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、增加方法

@RequestMapping("/")
public String helloworld(){
return "Hello world!";
}

@RequestMapping("/hello/{name}")
public String hellName(@PathVariable String Name){
return "Hello "+Name;
}

4、保存Ctrl+S

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

6、整个HelloController文件如下

package com.example;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

@RequestMapping("/")
public String helloworld(){
return "Hello world!";
}

@RequestMapping("/hello/{name}")
public String hellName(@PathVariable String name){
return "Hello "+name;
}
}
7、启动测试

在浏览器中依次输入
http://localhost:8080/ http://localhost:8080/hello/上帝



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: