学习SpringBoot的前提:使用过Maven、Spring

打开网址:https://spring.io/guides/gs/spring-boot-docker/

SpringBoot - HelloWorld

在eclipse中创建Maven项目

SpringBoot - HelloWorld

GroupId、ArtifactId、Version参考第一张图片,注意这里是jar,而不是war

SpringBoot - HelloWorld

点击finish,项目开始自动构建,然后在pom中加入

  <dependencies>
  	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>

 值得注意的:如果是SpringBoot2之前的版本,建议使用jdk1.7,SpringBoot2使用jdk1.8

然后写一个Controller和一个main函数

SpringBoot - HelloWorld

package cn.bl.controller;

import java.util.HashMap;
import java.util.Map;

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

@Controller
public class ShowHelloWorld {
	
	@RequestMapping("/hello")
	@ResponseBody
	public Map<String, String> show(){
		Map<String, String>map = new HashMap<>();
		map.put("a", "aa");
		map.put("b", "bb");
		return map;
	}
}
package cn.bl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

这个App要放在这个ShowHelloWorld的同一个包或者它的上层,否则报错

执行:也就是执行这个main

SpringBoot - HelloWorld

在火狐地址栏输入: http://localhost:8080/hello

SpringBoot - HelloWorld

这个helloWorld就算是完成了

相关文章: