명령줄 도구CURL

3914 단어 springbootRestful

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

 
 
 

좋은 웹페이지 즐겨찾기