版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq1021979964/article/details/88633064
SpringBoot入门Hello World
实现一个最基本的由SpringBoot方式编写的Hello World,就知道JavaWeb开发是多么方便了。我使用的是Maven。
如果不太明白怎么创建项目这些,可以看上一篇博客。https://blog.csdn.net/qq1021979964/article/details/88633155
pom.xml
<?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>
<name>01-spring-boot-test</name>
<description>Demo project for Spring Boot</description>
<groupId>com.kevin</groupId>
<artifactId>01-spring-boot-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- springboot版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<!--<parent>
<artifactId>spring-boot-kevin</artifactId>
<groupId>com.kevin</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>01-spring-boot-test</artifactId>-->
<dependencies>
<!-- 集成sparing mvc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
Controller
package com.kevin.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
/**
* @author kevin
* @version 1.0
* @description 控制类
* @createDate 2019/3/12
*/
@Controller
public class HelloWorld {
// 访问地址: http://localhost:8080/hello
@RequestMapping("/hello")
@ResponseBody
public Map<String,Object> showHelloWorld(){
HashMap<String, Object> map = new HashMap<>();
map.put("msg","HelloWorld");
return map;
}
}
Application
package com.kevin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author kevin
* @version 1.0
* @description 启动类
* @createDate 2019/3/12
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
访问地址:localhost:8080/hello
由SpringBoot开发的HelloWorld就这么可以了,是不是超级简单。对比一下Java中的用ssm或ssh,就算是单纯的springmvc,也需要一些配置文件,这里完全不需要。