我是一个小目录
一、springboot简介
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
二、restful接口相关简介
2.1 rest定义
REST(英文:Representational State Transfer,简称REST)描述了一个架构样式的网络系统,比如 web 应用程序。它首次出现在 2000 年 Roy Fielding 的博士论文中,Roy Fielding是 HTTP 规范的主要编写者之一。在目前主流的三种Web服务交互方案中,REST相比于SOAP(Simple Object Access protocol,简单对象访问协议)以及XML-RPC更加简单明了,无论是对URL的处理还是对Payload的编码,REST都倾向于用更加简单轻量的方法设计和实现。值得注意的是REST并没有一个明确的标准,而更像是一种设计的风格。
2.2 restful架构简介
RESTful架构是对MVC架构改进后所形成的一种架构,通过使用事先定义好的接口与不同的服务联系起来。在RESTful架构中,浏览器使用POST,DELETE,PUT和GET四种请求方式分别对指定的URL资源进行增删改查操作。因此,RESTful是通过URI实现对资源的管理及访问,具有扩展性强、结构清晰的特点。
RESTful架构将服务器分成前端服务器和后端服务器两部分,前端服务器为用户提供无模型的视图;后端服务器为前端服务器提供接口。浏览器向前端服务器请求视图,通过视图中包含的AJAX函数发起接口请求获取模型。
三、项目创建并运行
3.1 项目创建
本文使用IDEA做示例!!!
新建一个项目,并且选择spring initializr,选择default,点击next
到后面一个界面时需要注意一下Java的版本,建议选择Java8,如果要改名字记得在artifact这一栏改变,后面的name就会自动改变
3.2 项目结构和代码
该项目的功能比较简单,就是获取当前的数与进行运算。
项目的结构:
Count.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());
}
}
}
如果要改变名称或者包的名字不一样,记得改包名和类名,还有那个SpringbootApplication是自动生成的,不用管他。
3.3 运行及结果
上面的弄好了就可以直接点击运行了,运行结果如下图:
记住千万别关,后面还有用!!!!
四、总结
初次体验springroot,可以看出springboot的功能十分强大,而且也十分方便,特别是里面独有的@注释机制方便也容易让人理解。不过和我们平时的编程时不一样,注释@的作用和其他时候基本都不一样,而且有些地方不能缺。
postman测试接口点这里!