A Restful API


@Patavariable @RequestParm @RequestBody@RequestHeader

定义POJO

Order

import java.util.Date;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j 
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Order {
	public long orderID;
	public String orderName;
	public String des;
	public Date time;
	public List<Buyer> buyer;
}

Buyer

import lombok.Data;
@Data
public class Buyer {
	public String ID;
	public String name;
}

定义RestfulController

Get API for query

	//@RequestMapping(value="/order/{id}",method=RequestMethod.GET)
	@GetMapping("/order/{id}")
		public AjaxResponse getOrder(@PathVariable("id") Long id,@RequestHeader String auth) {
		log.info("running get car here by " + id +" auth:"+auth);
		Order order = Order.builder()
							.orderID(id)
							.orderName("TSL")
							.des("Eloon Mask is a superman")
							.build();
		return AjaxResponse.success(order,"the car belonged");
	}
running get car here by 1 auth:certification

在这里插入图片描述

Post API for add

	@PostMapping("/order")
	public AjaxResponse saveOrder(@RequestBody Order order) {
		log.info("Oder:" + order);
		return AjaxResponse.success(order);
	}
// or	
//	public AjaxResponse saveOrder(@RequestParam long orderID,
//								  @RequestParam String orderName,
//								  @RequestParam String des,
//								  @DateTimeFormat(pattern="yyyy-MM-dd hh:mm:ss")
//								  @RequestParam Date time) 
//	{
//		log.info("Order time:" + time);
//		return AjaxResponse.success(orderID);
//	}

在这里插入图片描述

Put API for update

	@PutMapping("/order")
	public AjaxResponse updateOrder(@RequestBody Order order) {
		log.info("Order:"+order);
		if(order.getOrderID()==0) {
			return AjaxResponse.parameterError(order, "OrderID should be fill in");
		}
		return AjaxResponse.success();
	}

在这里插入图片描述

Delete API for delete

	@DeleteMapping("/order/{id}")
	public AjaxResponse DeleteOrder(@PathVariable Long id) {
		log.info("Running deleteOrder ID:"+id);
		if(id == null) {
			AjaxResponse.parameterError(id, "ID should be fill in");
		}
		return AjaxResponse.success();
	}

在这里插入图片描述

定义AjaxResponse

package com.book.springtest;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class AjaxResponse {
	private boolean isOK;
	private int code;// 200,400,404,500
	private String message;
	private Object data;
	
	public static AjaxResponse success() {
		AjaxResponse ajaxResponse = new AjaxResponse();
		ajaxResponse.setOK(true);
		ajaxResponse.setCode(200);
		ajaxResponse.setMessage("This is normal");
		return ajaxResponse;
	}
	
	public static AjaxResponse success(Object obj) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		ajaxResponse.setOK(true);
		ajaxResponse.setCode(200);
		ajaxResponse.setMessage("This is normal");
		ajaxResponse.setData(obj);
		return ajaxResponse;
	}
	
	public static AjaxResponse success(Object obj,String message) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		ajaxResponse.setOK(true);
		ajaxResponse.setCode(200);
		ajaxResponse.setMessage(message);
		ajaxResponse.setData(obj);
		return ajaxResponse;
	}
	public static AjaxResponse parameterError(Object obj,String message) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		ajaxResponse.setOK(false);
		ajaxResponse.setCode(400);
		ajaxResponse.setMessage(message);
		ajaxResponse.setData(obj);
		return ajaxResponse;
	}	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值