RESTful Internet software architecture


foreword

提示:这里可以添加本文要记录的大概内容:

RESTful is also called REST, which is the abbreviation of "Representational State Transfer" in English.

GET is used to obtain resources;
POST is used to create new resources (and can also be used to update resources);
PUT is used to update resources;
DELETE is used to delete resources;

method corresponding annotation
adding data @PostMapping
delete data @DeleteMapping
change the data @PutMapping
query all data @GetMapping

提示:以下是本篇文章正文内容,下面案例可供参考

Project cases

po class code:

public class Shop {
    
    

	private Integer shopId;
	private String shopName;
	private String shopAddress;
	private String shopExplain;
	private Double salePrice;
	private Double deliveryPrice;

	// 自动生成 Getter、Setter、有参无参方法
	// 修改toString()格式
	@Override
	public String toString() {
    
    
		return "商家编号:" + shopId + "\t 商家名称:" + shopName + "\t 商家地址:" + shopAddress + "\t 商家介绍:"
				+ shopExplain + "\t 所售价格:" + salePrice + "\t 快递费:" + deliveryPrice ;
	}
	
}

Create a new controller package

@RestController  //相当于@Controller+@ResponseBody的组合
@RequestMapping("/shop")
public class ShopController {
    
    

}

Add data @PostMapping

	// 具体地址不用配置
	@PostMapping
	public String add(Shop shop) {
    
    
		System.out.println(shop+"增加成功");
		return "OK";
	}

Test:
insert image description here
console output: the data is corresponding
insert image description here

Delete data @DeleteMapping

	// 自定义名字
	@DeleteMapping("/{id}")
	public String delete(@PathVariable("id") int id) {
    
    
		System.out.println("编号是"+id+"的商家删除成功");
		return "OK";
	}

test:
insert image description here

Modify data @PutMapping

	@PutMapping
	public String update(Shop shop) {
    
    
		System.out.println(shop+"修改成功");
		return "OK";
	}

test:
insert image description here

Query all data @GetMapping

	@GetMapping
	public List<Shop> queryShops(){
    
    
		List<Shop> list = new ArrayList<Shop>();
		list.add(new Shop(1, "大盘鸡", "津海市广场对过左拐200米", "还不错", 15.0, 0.0));
		list.add(new Shop(2, "手工水饺", "津海市广场对过右拐100米", "挺好的", 20.0, 0.0));
		list.add(new Shop(2, "野菜饭馆", "津海市广场对过右拐100米左拐", "还行吧", 200.0, 0.0));
		return list;
	}

test:
insert image description here

Query data by ID @GetMapping("/{xxx}")

The path variable is custom, here it is ox

	@GetMapping("/{ox}")
	public Shop queryShopById(@PathVariable("ox") int id) {
    
    
		Shop shop = new Shop(id,"全牛宴", "津海市大学对过", "味道不错", 500.0, 0.0);
		return shop;
	}

Test: mock dynamic id
insert image description here

Complex multi-condition query @PostMapping("/xxx")

Here you need to add more and the path, here is the name qc

	//多条件查询(复杂)多加一条路径
	@PostMapping("/qc")
	public List<Shop> queryShopsCondition(Shop shop){
    
    
		System.out.println("查询条件是"+shop);
		
		List<Shop> list = new ArrayList<Shop>();
		list.add(new Shop(2, "野菜饭馆", "津海市广场对过右拐100米左拐", "还行吧", 200.0, 0.0));
		list.add(new Shop(2, "手工水饺", "津海市广场对过右拐100米", "挺好的", 20.0, 0.0));
		return list;
	}

test
insert image description here

Guess you like

Origin blog.csdn.net/rej177/article/details/132156842