Spring Boot 开发入门


前言

Spring Boot 开发入门


一、Spring Boot编写helloworld

1、首先使用IDEA创建一个Spring Boot项目文件
2、在web文件目录下建立一个新的html文件hello
3、在标题中将其修改为hello
4、添加正文为“helloword Spring Boot!这是一个用Spring Boot开发的网站”
请添加图片描述
5、点击右上角网页打开按钮

请添加图片描述
6、显示结果
请添加图片描述

二、RESTful 接口的 Web服务

1.RESTful 是什么

1、RESTful 的本质核心是一种软件架构,可以降低开发复杂性,提高系统可伸缩性。
2、http协议是属一种应用层协议结构为:
schema://host[:port]/path[?query-stringl][#anchor]

scheme:指定低层使用的协议(例如:http,https,ftp)
host:服务器的IP地址或者域名
port:服务器端口,默认为80,https默认端口为443
path:访问资源的路径
query-string:发送给htp服务器的数据
anchor:锚

三、创建Web项目

一,在com.example.demo中创建三个三个包并创建相应的类:
在这里插入图片描述
二、添加相应代码
Count:

package com.example.demo.bean;
 
public class Count {
    
    
    private int count;
 
    public int getCount() {
    
    
        return count;
    }
 
    public void setCount(int count) {
    
    
        this.count = count;
    }
}

ResourceController:

 import com.example.demo.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
@RestController
public class ResourceController {
    
    
 
    @Autowired
    ResourceService resourceService;
 
    @RequestMapping(value = "/me/count", method = RequestMethod.PUT)
    @ResponseBody
    public void initCount(@RequestBody Count count){
    
    
        resourceService.initCount(count);
    }
 
    @RequestMapping(value = "/me/count", method = RequestMethod.POST)
    @ResponseBody
    public void modifyCount(@RequestBody Count count){
    
    
        resourceService.addCount(count);
    }
 
    @RequestMapping(value = "/me/count", method = RequestMethod.GET)
    @ResponseBody
    public  Count getCount()
    {
    
    
        return resourceService.getCount();
    }
}

ResourceManager:

package com.example.demo.manager;
 
public class ResourceManager {
    
    
    private int count = 0;
 
    private static ResourceManager instance = new ResourceManager();
 
    private ResourceManager(){
    
    }
 
    public static ResourceManager 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;
    }
}

ResourceService:

扫描二维码关注公众号,回复: 13494070 查看本文章
package com.example.demo.service;
 
import com.example.demo.bean.Count;
import com.example.demo.manager.ResourceManager;
import org.springframework.stereotype.Service;
 
@Service
public class ResourceService {
    
    
    public void addCount(Count count){
    
    
        if (count != null){
    
    
            ResourceManager.getInstance().addCount(count.getCount());
        }
    }
 
    public void minusCount(Count count){
    
    
        if (count != null) {
    
    
            ResourceManager.getInstance().minusCount(count.getCount());
        }
    }
 
    public Count getCount()
    {
    
    
        Count count = new Count();
        count.setCount(ResourceManager.getInstance().getCount());
        return count;
    }
 
    public void initCount(Count count){
    
    
        if (count != null) {
    
    
            ResourceManager.getInstance().initCount(count.getCount());
        }
    }
}

三、运行结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/changlingMYlove/article/details/120392864
今日推荐