springboot建立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 {
    @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才会正确识别。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot一个用于快速开发Spring应用程序的框架,其中包含了很多的自动化配置,它建立Spring之上,使得开发人员能够更快、更轻松地开发出高品质的应用程序。Spring Boot通过提供默认配置和快速应用程序启动来简化了开发人员的开发流程,同时也提供了一种更加灵活的方式开发RESTful API。 下面是如何使用Spring Boot编写RESTful API的步骤: 1. 第一步是创建Spring Boot项目。你可以使用Spring Initializr来创建练习项目。在项目创建的时候,你需要添加web依赖,因为它是构建RESTful API所必须的。 2. Spring Boot通过自动配置来创建一个内嵌的Tomcat服务器,你不需要独立安装Tomcat服务器。在创建完项目之后,Spring Boot会创建一个默认的Application类,你可以在这里创建一个RestController来处理HTTP请求。使用@RestController注释指示Spring将该类视为RESTful资源的控制器。这样,Spring Boot将会根据类的注释自动配置现有的服务,包括它所需要的绑定。 3. 我们可以在类中使用@GetMapping、@PostMapping、@DeleteMapping和@PutMapping注释来定义RESTful路由。这些注释用于处理HTTP GET、POST、DELETE和PUT请求。我们可以在这里定义要接受的值、路径和它返回的内容。 4. 在创建完RESTful API之后,你需要使用一些测试工具来测试它是否工作。你可以使用Postman、CURL或者浏览器来测试API是否可用。 总之,Spring Boot让编写RESTful API变得更加容易和高效。这个框架可以自动化设置Web服务、路由和其他服务,使得服务器开发人员能够在更短的时间内开发出高质量的RESTful API。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值