使用IDEA创建一个spring boot服务,并创建三个restful接口
目录一、新建项目1、选择Spring Initializr,点击next。2、输入项目组名及项目名称,java version版本选择8,点击next。3、选择Spring Web,然后finish二、编码1、创建如图所示的类2、Count3、DemoApplication4、ResourceController5、ResourceManager6、ResourceService7、运行程序8、在
·
目录
一、新建项目
1、选择Spring Initializr,点击next。
2、输入项目组名及项目名称,java version版本选择8,点击next。
3、选择Spring Web,然后finish
二、编码
1、创建如图所示的类
2、Count
package com.example.demo;
public class Count {
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
3、DemoApplication
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4、ResourceController
package com.example.demo;
import com.example.demo.Count;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.demo.ResourceService;
@RestController
public class ResourceController {
@Autowired
ResourceService resourceService;
@RequestMapping(value = "/count", method = RequestMethod.PUT)
@ResponseBody
public void initCount(@RequestBody Count count){
resourceService.initCount(count);
}
@RequestMapping(value = "/count", method = RequestMethod.POST)
@ResponseBody
public void modifyCount(@RequestBody Count count){
resourceService.addCount(count);
}
@RequestMapping(value = "/count", method = RequestMethod.GET)
@ResponseBody
public Count getCount()
{
return resourceService.getCount();
}
}
5、ResourceManager
package com.example.demo;
public class ResourceManager {
private int count = 0;
private static ResourceManager instance = new ResourceManager();
private ResourceManager(){}
public static ResourceManager 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;
}
}
6、ResourceService
package com.example.demo;
import com.example.demo.Count;
import com.example.demo.ResourceManager;
import org.springframework.stereotype.Service;
@Service
public class ResourceService {
public void addCount(Count count){
if (count != null){
ResourceManager.getInstance().addCount(count.getCount());
}
}
public void minusCount(Count count){
if (count != null) {
ResourceManager.getInstance().minusCount(count.getCount());
}
}
public Count getCount()
{
Count count = new Count();
count.setCount(ResourceManager.getInstance().getCount());
return count;
}
public void initCount(Count count){
if (count != null) {
ResourceManager.getInstance().initCount(count.getCount());
}
}
}
7、运行程序
8、在浏览器地址栏上输入localhost:8080/count
三、测试
本文采用Postman 8.12.2 版本进行测试。
服务提供三个接口,URL均为:http://localhost:8080/count
put进行初始化,post用于修改(这里修改是累加),get用于查询。
1、如何处理get请求
在Postman的工作区中:
1、选择HTTP请求方式为GET
2、在URL区域输入链接
3、点击 “Send”按钮
4、你将看到下方返回200状态码
5、在正文中出现相应结果,表明您的测试已经成功运行。
2、如何处理post请求
(1)创建一个新请求
(2)在新请求中
1、选择HTTP请求方式为post
2、在URL区域输入链接:http://localhost:8080/count
3、切换到Body选项
4、选中raw选项
5、选择JSON
6、复制前面GET请求返回的json内容,并将“0”改为“10”,点击send
7、查询
3、如何处理put请求
1、选择HTTP请求方式为put
2、在URL区域输入链接:http://localhost:8080/count
3、切换到Body选项
4、选中raw选项
5、选择JSON
6、复制前面GET请求返回的json内容,并将“0”改为“140”,点击send
7、查询
更多推荐
已为社区贡献1条内容
所有评论(0)