명령줄 도구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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin Springboot -- 파트 14 사용 사례 REST로 전환하여 POST로 JSON으로 전환前回 前回 前回 記事 の は は で で で で で で を 使っ 使っ 使っ て て て て て リクエスト を を 受け取り 、 reqeustbody で 、 その リクエスト の ボディ ボディ を を 受け取り 、 関数 内部 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.