- 一、利用Spring boot在网页中输出Helloworld
- 1.新建一个项目
- 2.选择java8
- 3.选择web和Spring web
- 4.给项目命名
- 5.创建一个网页
- 6.启动项目
- 二、GET、PUT和POST请求处理
- (1)HTTP基础知识
- (2)项目代码
- 1.项目的创建
- 2.项目内容
- 3.核心代码
- (3)测试
- 1.测试get
- 2.测试post
- 3.测试put
项目创建完毕后还需要等待下载资源
5.创建一个网页在com.example.demo目录下新建一个package,然后在新的package中新建一个Java类
Helloworld类代码如下
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloWorld {
@ResponseBody
@RequestMapping("/hello")//用于访问网页的,后面用到的http://localhost:8080/hello中的hello来源
public String hello(){//网页内容
return "Hello World!";
}
}
6.启动项目
在DemoApplication类中右击后运行
出现这个就意味着运行成功
打开浏览器输入http://localhost:8080/hello即可看到如下效果
二、GET、PUT和POST请求处理 (1)HTTP基础知识参考:https://www.jianshu.com/p/9b8b9672b8e6
(2)项目代码 1.项目的创建参考前文
2.项目内容通过http不同的请求来进行对数字进行相应的操作。
当发出get请求的时候对数字进行初始化为0。 当发出post请求的时候对数字进行相加操作。 当发出put请求的时候把数字重置为指定值。3.核心代码
项目结构
负责处理http请求的NumberWeb类
根据不同的请求来执行不同的函数,最后返回执行结果。
package com.example.demo.controller;
import com.example.demo.operation.NumberOperation;
import com.example.demo.service.NumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class NumberWeb {
@Autowired
@ResponseBody
@RequestMapping(value = "/index",method = RequestMethod.GET)
public String init(){
NumberService.init();
return "初始化为: "+NumberService.get();
}
@ResponseBody
@RequestMapping(value = "/index",method = RequestMethod.POST)
public String get(int n){
NumberService.add(n);
return "新的数字: "+NumberService.get();
}
@ResponseBody
@RequestMapping(value = "/index",method = RequestMethod.PUT)
public String set(int n){
NumberService.set(n);
return "新的数字: "+NumberService.get();
}
}
负责对数字进行相关操作的NumberService类
主要负责对调用NumberOperation的对应方法
package com.example.demo.service;
import com.example.demo.operation.NumberOperation;
public class NumberService {
public static void add(int n){
NumberOperation.getNumberOperation().add(n);
}
public static int get(){
return NumberOperation.getNumberOperation().getNumber();
}
public static void set(int n){
NumberOperation.getNumberOperation().setNumber(n);
}
public static void init(){
NumberOperation.getNumberOperation().init();
}
}
操作NumberService的NumberOperation类
主要提供操作数字的相关方法。
package com.example.demo.operation;
public class NumberOperation {
private int number;//数字
private static NumberOperation numberOperation = new NumberOperation();//接口
private NumberOperation(){
}
public static NumberOperation getNumberOperation() {
return numberOperation;
}
public void setNumber(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
public synchronized void add(int n){
this.number+=n;
}
public void init(){
this.number=0;
}
}
(3)测试
使用postman软件进行模拟请求
1.测试get当发出get请求的时候,可以看到数字初始化为0。
2.测试post把请求改为post,同时连续请求两次,参数值分别为5和4。
可以发现每一次请求后n的值都增加了对应的值。
3.测试put把请求改为put,同时参数改为3可以发现值也变为了3。



