[] SpringBootサービスを作成するためのアイデアを使用し、3つの安らかスタイルインターフェイスを作成SpringBoot

プロジェクトの作成:

プロジェクトを作成することを選択しSpringboot:

一般的に、プロジェクトの情報を入力し、情報Mavenを記入することです。

春のWebスターターを選択します。

[OK]を終了します。

コード:

デモ機能は、カウンタ機能を提供することで、クエリのカウンタ現在値、カウンタを変更し、カウンタを初期化することができます。いいえデータベースは直接実施形態の一つのクラスは、以下の項目の構造をシミュレートするために、使用されません。

カウント:

1つの パッケージcom.me.redis.resouce.bean。
2  
3  パブリック クラスカウント{
 4      プライベート int型の数。
5  
6      公共 のint 同様にgetCount(){
 7          リターンがカウント。
8      }
 9  
10      公共 ボイド setCount(INTの数){
 11          この .count = 数えます。
12      }
 13 }

 

ResourceController:

 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 }

 

ResourceService:

 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 }

 

ResourceManager:

 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 }

 

ResouceApplication是idea自动生成的:

 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 }

代码就是这样,注意几个注解的作用就行了。

 

启动服务:

在ResourceApplication类上右键启动:

服务启动正常:

 

测试:

服务提供了三个接口:

URL都是:/me/count 只是分PUT、POST和GET,其中PUT用于初始化,POST用于修改(这里修改是累加),GET用于查询。

下面使用POSTMan进行测试:

查询接口,服务启动,count默认就是0:

 

初始化:

 

再次使用查询接口:

 

 修改接口:

修改后查询:

加一个负数试试:

查询:

 

OK,使用SpringBoot开发restful风格的接口完成。

おすすめ

転載: www.cnblogs.com/wuyizuokan/p/11117294.html