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

SpringBoot入门学习(一): Idea 创建 SpringBoot 的 HelloWorld

2017-08-16 09:47 429 查看
创建项目:











项目结构:



程序启动入口:



正式开始:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.example.helloweb") //扫描所需要的包
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}


HelloWorld:

package com.example.helloweb;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
public class HelloWord {

/**
* @Controller 和 @ResponseBody 配合相当于 @RestController,
* 返回前端页面的都是 json 格式数据。
* @return
*/
@RequestMapping("/hello")
public @ResponseBody String index () {
System.out.println("欢迎进入SpringBoot");
return "Hello World!";
}
}


控制台显示:

欢迎进入SpringBoot
页面显示:





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