springboot建立restful接口
目录
- restful简介
- 项目创建及运行
- 总结
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 {@AutowiredspringbootService springbootservice;@RequestMapping(value = "/me/count", method = RequestMethod.PUT)@ResponseBodypublic void initCount(@RequestBody Count count){springbootservice.initCount(count);}@RequestMapping(value = "/me/count", method = RequestMethod.POST)@ResponseBodypublic void modifyCount( @RequestBody Count count){springbootservice.addCount(count);}@RequestMapping(value = "/me/count", method = RequestMethod.GET)@ResponseBodypublic 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才会正确识别。
springboot建立restful接口相关推荐
- c#分页_使用Kotlin搭配Springboot开发RESTFul接口(二)自定义配置、跨域、分页
前言 上一篇文章请看这里:使用Kotlin搭配Springboot开发RESTFul接口与服务部署 上一篇文章介绍了Kotlin搭配Springboot的开发流程,从搭建项目.ORM.Controll ...
- SpringBoot开发Restful接口
1.说明 基于已经创建好的Spring Boot工程, 开发Restful风格的接口, 并且对外提供HTTP服务. Spring Boot工程创建方式有两种: Maven向导方式:SpringBoot ...
- SpringBoot开发Restful风格的接口实现CRUD功能
一.前言 我们都知道SpringBoot的出现使得在开发web项目的时候变得更加方便.快捷.之前写过一篇文章是如何快速搭建一个springboot项目:SpringBoot入门:使用IDEA和Ecli ...
- vue项目构建实战基础知识:SPA理解/RESTful接口介绍/static目录配置/axios封装/打包时map文件去除...
一.SPA 不是指水疗.是 single page web application 的缩写.中文翻译为 单页应用程序 或 单页Web应用,更多解释请自行搜索. 所有的前端人员都应该明白我们的页面的 u ...
- python框架 mysql数据库_在Python的框架中为MySQL实现restful接口的教程
最近在做游戏服务分层的时候,一直想把mysql的访问独立成一个单独的服务DBGate,原因如下: 请求收拢到DBGate,可以使DBGate变为无状态的,方便横向扩展 当请求量或者存储量变大时,mys ...
- SpringBoot调用RESTful Web服务
SpringBoot调用RESTful Web服务 本指南将引导您完成创建使用RESTful Web服务的应用程序的过程. 你会建立什么 您将RestTemplate在https://gturnqui ...
- Swagger与Springboot集成restful进行接口测试
Swagger集成到SpringBoot中实现restful编程中动态调试接口 导入maven依赖 <!-- swagge 依赖 包含地址值 http://localhost:8080/spri ...
- SpringCloud工作笔记042---SpringCloud RestFul接口中跨域问题_这个解决方式不太完美,每个接口上都要加
JAVA技术交流QQ群:170933152 下面是找的,我们在springcloud项目中,使用: Restful接口,有些put类型,delete类型的接口,是有跨域问题,访问不到的,这个时候 需要 ...
- java+swagger+侵入_Spring boot+Swagger配置无侵入式Restful接口(一)
最近一直遇到有人问我spring boot +swagger怎么配置无侵入式的restful接口,这段时间一直很忙,没有时间,刚好星期六,趁着休息时间给大家写个demo,在这里我就粘贴一些步骤,具体的 ...
最新文章
- Xamarin.Forms教程下载安装Visual Studio 2015
- 分布式唯一ID生成器
- 求最大子段和的一些算法
- day06 hashlib模块
- 【kafka】kafka record is corrupt(记录损坏)
- context c语言作用,理解 Go context
- 过滤代码中的html标签
- execl执行linux命令,Excel 调用Shell命令执行bash脚本和命令行代码
- Smart3D基础理论
- 模板template基础学习
- 1102: 韩信点兵
- XDUOJ题解合集(待填坑)
- 2022-10-24 ClickHouse 源码解析-查询引擎经典理论
- K.im团队与Kim Dotcom AMA直播回顾
- 全民程序员时代来了吗?解锁低代码流行背后的秘密
- 忽略Xgboost的Warning
- 创造习惯 — 如何科学地养成习惯
- 2021-最新Web前端经典面试试题及答案-史上最全前端面试题(含答案)---手写代码篇
- java 改变图片的DPI
- console连接h3c s5500_H3C交换机console口进入怎么设置密码