主要实现功能:浏览器发送/hello请求,服务器接受请求并处理,输出hello world字符串。
1.新建maven项目
2.pom.xml中加入starter和相关依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<!--<relativePath/> <!– lookup parent from repository –>-->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.编写一个主程序,启动Springboot应用
helloworlddemo.java
package com.yzl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//来标注一个主程序类,说明这是一个SpringBoot应用
@SpringBootApplication
public class helloworlddemo {
public static void main(String[] args) {
//spring应用启动起来
SpringApplication.run(helloworlddemo.class,args);
}
}
4.编写Controller
package com.yzl.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello (){
return "HELLO WORLD";
}
}
5.启动主程序
浏览器通过http://localhost:8080/hello访问,输出:
HELLO WORLD
网友评论