命令行工具CURL

基于spring-boot的restful

先贴个后台代码

package com.example.rest;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/student")
public class StudentController {
	private static final Log log = LogFactory.getLog(StudentController.class);
	
	@GetMapping
	public List getAll(){
		
		if(log.isInfoEnabled()) {
			log.trace("getAll()被调用了");
		}
		List stuList = new ArrayList();
		Student stu1 = new Student(1,"张三",new Date(),true);
		Student stu2 = new Student(2,"李斯",new Date(),false);
		Student stu3 = new Student(3,"王五",new Date(),true);
		Student stu4 = new Student(4,"赵刘",new Date(),true);
		stuList.add(stu1);
		stuList.add(stu2);
		stuList.add(stu3);
		stuList.add(stu4);
		return stuList;
	}
	
	@GetMapping("{id}")
	public Student getOne(@PathVariable int id) {
		if(log.isInfoEnabled()) {
			log.trace("getOne,id=="+id);
		}
		if(id==101) {
			return new Student(2,"李斯",new Date(),false);
		}else {
			throw new ResourceNotFoundException();
		}
	}
	
	@PostMapping
	public Student addStu(@RequestBody Student student) {
		if(log.isInfoEnabled()) {
			log.trace("这里因该写新增student到数据库的代码==" + student.toString());
		}
		student.setId(789);
		return student;
	}
	
	@PutMapping("{id}")
	public Student updateStu(@PathVariable int id, @RequestBody Student student) {
		if(log.isInfoEnabled()) {
			log.trace("这里因该写更新student到数据库的代码==" + student.toString());
		}
		if(id == 1011) {
			return student;
		}else {
			throw new ResourceNotFoundException();
		}
	}
	
	// required默认等于ture,当等于ture时,value值不传,会报400错误
	@DeleteMapping("{id}")
	public Map delStu(@PathVariable int id, HttpServletRequest request, @RequestParam(value="delete_reason", required=false) String deleteReason) throws Exception{
		if(log.isInfoEnabled()) {
			log.trace("这里因该写删除student到数据库的代码id==" + id);
		}
		Map map = new HashMap();
		
		if(id==100) {
			map.put("message", "id为"+id+"的学生已经被删除,删除原因="+deleteReason);
		} else if(id==103){
			// RuntimeException不如org.springframwork.security.access.AccessDeniedException合适
			throw new RuntimeException("#100不能被删除");
		}else {
			// 不存在
			throw new ResourceNotFoundException();
		}
		
		return map;
	}
	
}

用命令行测试:

GET 请求:curl http://localhost:8080/student
POST 请求:curl -H "Content-Type:application/json" -X POST --data '{"name":"West World","creatDate":"2016-08-22","used":"true"}' http://localhost:8080/student
DELET 请求: curl -X DELECT https://localhost:8080/student/100/
PUT 请求:curl -H "Content-Type:application/json" -X PUT -data '{"name":"West World","creatDate":"2016-08-22","used":"true"}' http://localhost:8080/student/1

 

 

 

 

你可能感兴趣的:(springboot,Restful)