【SpringBoot】使用IDEA创建一个SpringBoot服务,并创建三个restful风格的接口
2020-12-13 04:39
标签:pos tco stat spring this not 初始 autowired 生成 选择创建一个springboot项目: 输入一下项目信息,一般就是maven的信息填一下: 选择spring web starter: 然后finish就OK了。 演示的功能就是提供一个计数器功能,可以初始化计数器,修改计数器,查询计数器当前值。没有使用数据库,直接用一个单例类来模拟了,项目结构如下: Count: ResourceController: ResourceService: ResourceManager: ResouceApplication是idea自动生成的: 代码就是这样,注意几个注解的作用就行了。 在ResourceApplication类上右键启动: 服务启动正常: 服务提供了三个接口: URL都是:/me/count 只是分PUT、POST和GET,其中PUT用于初始化,POST用于修改(这里修改是累加),GET用于查询。 下面使用POSTMan进行测试: 查询接口,服务启动,count默认就是0: 初始化: 再次使用查询接口: 修改接口: 修改后查询: 加一个负数试试: 查询: OK,使用SpringBoot开发restful风格的接口完成。 【SpringBoot】使用IDEA创建一个SpringBoot服务,并创建三个restful风格的接口 标签:pos tco stat spring this not 初始 autowired 生成 原文地址:https://www.cnblogs.com/wuyizuokan/p/11117294.html项目创建:
编码:
1 package com.me.redis.resouce.bean;
2
3 public class Count {
4 private int count;
5
6 public int getCount() {
7 return count;
8 }
9
10 public void setCount(int count) {
11 this.count = count;
12 }
13 }
1 package com.me.redis.resouce.controller;
2
3 import com.me.redis.resouce.bean.Count;
4 import org.springframework.beans.factory.annotation.Autowired;
5 import org.springframework.web.bind.annotation.*;
6 import com.me.redis.resouce.service.ResourceService;
7
8 @RestController
9 public class ResourceController {
10
11 @Autowired
12 ResourceService resourceService;
13
14 @RequestMapping(value = "/me/count", method = RequestMethod.PUT)
15 @ResponseBody
16 public String initCount(@RequestBody Count count){
17 resourceService.initCount(count);
18 return "init count success";
19 }
20
21 @RequestMapping(value = "/me/count", method = RequestMethod.POST)
22 @ResponseBody
23 public String modifyCount(@RequestBody Count count){
24 resourceService.addCount(count);
25 return "modify count success";
26 }
27
28 @RequestMapping(value = "/me/count", method = RequestMethod.GET)
29 @ResponseBody
30 public Count getCount()
31 {
32 return resourceService.getCount();
33 }
34 }
1 package com.me.redis.resouce.service;
2
3 import com.me.redis.resouce.bean.Count;
4 import com.me.redis.resouce.manager.ResourceManager;
5 import org.springframework.stereotype.Service;
6
7 @Service
8 public class ResourceService {
9 public void addCount(Count count){
10 if (count != null){
11 ResourceManager.getInstance().addCount(count.getCount());
12 }
13 }
14
15 public void minusCount(Count count){
16 if (count != null) {
17 ResourceManager.getInstance().minusCount(count.getCount());
18 }
19 }
20
21 public Count getCount()
22 {
23 Count count = new Count();
24 count.setCount(ResourceManager.getInstance().getCount());
25 return count;
26 }
27
28 public void initCount(Count count){
29 if (count != null) {
30 ResourceManager.getInstance().initCount(count.getCount());
31 }
32 }
33 }
1 package com.me.redis.resouce.manager;
2
3 public class ResourceManager {
4 private int count = 0;
5
6 private static ResourceManager instance = new ResourceManager();
7
8 private ResourceManager(){}
9
10 public static ResourceManager getInstance(){
11 return instance;
12 }
13
14 public synchronized void addCount(int i){
15 count = count + i;
16 }
17
18 public synchronized void minusCount(int i){
19 count = count -i;
20 }
21
22 public int getCount(){
23 return count;
24 }
25
26 public void initCount(int i){
27 count = i;
28 }
29 }
1 package com.me.redis.resouce;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5
6 @SpringBootApplication
7 public class ResouceApplication {
8
9 public static void main(String[] args) {
10 SpringApplication.run(ResouceApplication.class, args);
11 }
12
13 }
启动服务:
测试:
文章标题:【SpringBoot】使用IDEA创建一个SpringBoot服务,并创建三个restful风格的接口
文章链接:http://soscw.com/essay/29710.html