SpringBoot 定义Restful API
- 定义POJO
- Order
- Buyer
 
- 定义RestfulController
- Get API for query
- Post API for add
- Put API for update
- Delete API for delete
 
- 定义AjaxResponse
 
@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;
	}	
}



















