ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot建立restful接口

2021-09-20 20:30:55  阅读:205  来源: 互联网

标签:count Count springboot springbootManager 接口 注解 restful public


目录

restful简介

restful架构简介:
RESTful架构是对MVC架构改进后所形成的一种架构,通过使用事先定义好的接口与不同的服务联系起来。在RESTful架构中,浏览器使用POST,DELETE,PUT和GET四种请求方式分别对指定的URL资源进行增删改查操作。因此,RESTful是通过URI实现对资源的管理及访问,具有扩展性强、结构清晰的特点。RESTful架构将服务器分成前端服务器和后端服务器两部分,前端服务器为用户提供无模型的视图;后端服务器为前端服务器提供接口。浏览器向前端服务器请求视图,通过视图中包含的AJAX函数发起接口请求获取模型。

项目创建及运行

1.新建一个项目,并且选择spring initializr,选择default,点击next在这里插入图片描述

2.注意一下Java的版本,建议选择Java8,可更改项目名字
在这里插入图片描述
3.选择springweb,然后选择存放路径在这里插入图片描述
在这里插入图片描述
4.建成后添加package和class,最后如下
在这里插入图片描述
5.在添加的class文件中输入代码,分别如下:
Cout.java:

package com.example.springboot.bean;

public class Count {
    private int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

springbootController.Java:

package com.example.springboot.controller;

import com.example.springboot.bean.Count;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.springboot.service.springbootService;

@RestController
public class springbootController {
    @Autowired
    springbootService springbootservice;
    @RequestMapping(value = "/me/count", method = RequestMethod.PUT)
    @ResponseBody
    public void initCount(@RequestBody Count count){
        springbootservice.initCount(count);
    }

    @RequestMapping(value = "/me/count", method = RequestMethod.POST)
    @ResponseBody
    public void modifyCount( @RequestBody Count count){
        springbootservice.addCount(count);
    }

    @RequestMapping(value = "/me/count", method = RequestMethod.GET)
    @ResponseBody
    public  Count getCount()
    {
        return springbootservice.getCount();
    }
}

springbootManager.Java:

package com.example.springboot.manager;

public class springbootManager {
    private int count = 0;

    private static springbootManager instance = new springbootManager();

    private springbootManager(){}

    public static springbootManager 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;
    }
}

springbootService.Java:

package com.example.springboot.service;
import com.example.springboot.bean.Count;
import com.example.springboot.manager.springbootManager;
import org.springframework.stereotype.Service;

@Service
public class springbootService {
    public void addCount(Count count){
        if (count != null){
            springbootManager.getInstance().addCount(count.getCount());
        }
    }

    public void minusCount(Count count){
        if (count != null) {
            springbootManager.getInstance().minusCount(count.getCount());
        }
    }

    public Count getCount()
    {
        Count count = new Count();
        count.setCount(springbootManager.getInstance().getCount());
        return count;
    }

    public void initCount(Count count){
        if (count != null) {
            springbootManager.getInstance().initCount(count.getCount());
        }
    }
}

6.点击运行,出现下图所示,则意味成功
在这里插入图片描述

总结

通过此次课程学习,我了解到springboot的功能十分强大,而且也十分方便,同时查阅资料了解到,SpringBoot一些常用的注解:
(1)@RestController和@Controller指定一个类,作为控制器的注解 ,并说明其区别
(2)@RequestMapping方法级别的映射注解,这一个用过Spring MVC的小伙伴相信都很熟悉
(3)@EnableAutoConfiguration和@SpringBootApplication是类级别的注解,根据maven依赖的jar来自动猜测完成正确的spring的对应配置,只要引入了spring-boot-starter-web的依赖,默认会自动配置Spring MVC和tomcat容器
(4)@Configuration类级别的注解,一般这个注解,我们用来标识main方法所在的类,完成元数据bean的初始化。
(5)@ComponentScan类级别的注解,自动扫描加载所有的Spring组件包括Bean注入,一般用在main方法所在的类上
(6)@ImportResource类级别注解,当我们必须使用一个xml的配置时,使用@ImportResource和@Configuration来标识这个文件资源的类。
(7)@Autowired注解,一般结合@ComponentScan注解,来自动注入一个Service或Dao级别的Bean
(8)@Component类级别注解,用来标识一个组件,比如我自定了一个filter,则需要此注解标识之后,Spring Boot才会正确识别。

标签:count,Count,springboot,springbootManager,接口,注解,restful,public
来源: https://blog.csdn.net/louderIII/article/details/120394068

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有