RESTful 接口的 Web服务
一、使用IntellijIDEA建立第一个spring boot 项目
1、创建项目
打开IDEA,点击file–>new–>project:
左边选择spring initializr,再next:
group和artifact可以自己取,然后把java version改成8,点击next:
选择web,再选web spring,点击next,这样就创建完成了:
创建完成了后,再创建4个包,每个包 里建一个java文件(如下图):
2、代码和程序调试
Count的代码:
package com.helloworld.boothello.bean;
public class Count {
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
Manage的代码:
package com.helloworld.boothello.manager;
public class Manager {
private int count = 0;
private static Manager instance = new Manager();
private Manager(){}
public static Manager getInstance(){
return instance;
}
public synchronized void addCount(int i){
count = count + i;
}
public synchronized void minusCount(int i){
count = count -i;
}
public int getCount(){
return count;
}
public void initCount(int i){
count = i;
}
}
Control的代码:
package com.helloworld.boothello.Controller;
import com.helloworld.boothello.bean.Count;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.helloworld.boothello.Service.service;
@RestController
public class Control {
@Autowired
service Services;
@RequestMapping(value = "/me/count", method = RequestMethod.PUT)
@ResponseBody
public void initCount(@RequestBody Count count){
Services.initCount(count);
}
@RequestMapping(value = "/me/count", method = RequestMethod.POST)
@ResponseBody
public void modifyCount(@RequestBody Count count){
Services.addCount(count);
}
@RequestMapping(value = "/me/count", method = RequestMethod.GET)
@ResponseBody
public Count getCount()
{
return Services.getCount();
}
}
service的代码:
package com.helloworld.boothello.Service;
import com.helloworld.boothello.bean.Count;
import com.helloworld.boothello.manager.Manager;
import org.springframework.stereotype.Service;
@Service
public class service {
public void addCount(Count count){
if (count != null){
Manager.getInstance().addCount(count.getCount());
}
}
public void minusCount(Count count){
if (count != null) {
Manager.getInstance().minusCount(count.getCount());
}
}
public Count getCount()
{
Count count = new Count();
count.setCount(Manager.getInstance().getCount());
return count;
}
public void initCount(Count count){
if (count != null) {
Manager.getInstance().initCount(count.getCount());
}
}
}
BootHelloApplication的代码是创建后就有的,不用改变,右键调试代码,输出可以看到端口是8888:
二、用postman来测试
web网页的测试也可以用postman软件来测试,在客户端可以分别用get、put、post、delete等测试该web服务资源时,服务器能做出正确的结果,推荐在postman官网下载。
get的应用
get–>send,得到count初值为0:
post的应用
可以改变初值,按下图步骤可以给count改变初值:
然后用get查看count的值,发现变成了我们赋值的值:
put的应用
put是改变值,将值改变再send:
用get查一下:
也可以加一个负数:
再用get查一下:
delete的应用
删除肯定有相应的参数传给后端,比如id,根据记录的id进行删除,在这里想要提一下物理删除和逻辑删除:
物理删除:那就是,把数据彻底的删除掉,数据库就不会存在那条数据啦;
逻辑删除:数据表会有个is delete字段,当想要删除的时候,将它设为Y,当需要提供数据给前端的时候遍历这个字段,若是已经是Y的那就不能返回给前端。
逻辑删除是一个保留数据的很好方式,邮件也会有那种,删除,还可以回复的那种,但是彻底删除那就不可以了,对一个公司来说,删除掉的数据也是有用的,还是可以进行数据分析的。
三、参考链接
1、字母哥博客:手摸手教你学spring boot 2.x
2、Spring Boot 使用IDEA创建一个 Spring Boot 服务,并创建三个restful风格的接口
3、postman的四种请求:post,get,put,delete