多语言展示
当前在线:1185今日阅读:75今日分享:44

SpringBoot入门程序

SpringBoot入门程序,我们程序的功能是浏览器发送hello请求,服务器接受请求并处理,相应hello world字符串。
工具/原料
1

SpringBoot

2

Intellij idea

方法/步骤
1

创建一个Maven工程:

2

导入SpringBoot相关的依赖包:        org.springframework.boot        spring-boot-starter-parent        2.0.0.RELEASE                            org.springframework.boot            spring-boot-starter-web           

3

编写一个主程序,启动Spring Boot应用。package com.gwolf;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@SpringBootApplicationpublic class SpringHelloMainApplication {      public static void main(String[] args) throws Exception {            SpringApplication.run(SpringHelloMainApplication.class, args);        }}

4

编写相关的Controller、Service。package com.gwolf.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class HelloController {        @ResponseBody    @RequestMapping('/hello')    public String hello() {        return 'hello World!';    }}

5

启动主程序的main方法启动应用。

6

在浏览器中访问地址:http://localhost:8080/hello

7

springboot打包部署也是相当简单,只需要导入一个插件。将这个应用打成一个jar包可以直接使用java -jar命令进行执行。                                    org.springframework.boot                spring-boot-maven-plugin                       

推荐信息