spring rest 增删改查(put\delete\post\get)简单示例详细讲解
(1)类HelloController
package com.demo.java;
import java.util.List;
import javax.websocket.server.PathParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value="/hello")
public class HelloController {
/**
* 测试链接:http://localhost:8080/springmvc_demo/hello/gettest1?name=xiaokeai
* 测试结果:gettest1 was invoked.hello xiaokeai
*/
@RequestMapping(value="/gettest1",method=RequestMethod.GET)
public String gettest1(@PathParam("name") String name){
return "gettest1 was invoked.hello " + name;
}
/**
*测试链接:http://localhost:8080/springmvc_demo/hello/gettest2/user1/25
*测试结果:gettest2 was invoked.hello user1,age 25
*/
@RequestMapping(value="/gettest2/{name}/{age}",method=RequestMethod.GET)
public String gettest2(@PathVariable String name,@PathVariable int age){
return "gettest2 was invoked.hello " + name + ",age " + age;
}
/**
* 测试链接:http://localhost:8080/springmvc_demo/hello/posttest1
* 测试header:Content-Type=application/json
* 测试body:{"id":"111","name":"xiaokeai","age":26}
* 控制台输出:update.new age is 29.
* 测试结果:{"id":"111","name":"xiaokeai","age":29}
*/
@RequestMapping(value = "/posttest1", method = RequestMethod.POST,
consumes = "application/json")
@ResponseBody
public TeacherVO posttest1(@RequestBody TeacherVO t) {
System.out.println("update.new age is 29.") ;
t.setAge(29) ;
return t;
}
/**
* 测试链接:http://localhost:8080/springmvc_demo/hello/puttest1
* 测试header:Content-Type=application/json
* 测试body:{"id":"111","name":"xiaokeai","age":26}
* 控制台输出:insert.t:Teacher [id=111, name=xiaokeai, age=26]
* 测试结果:1
*/
@RequestMapping(value = "/puttest1", method = RequestMethod.PUT)
public int puttest1(@RequestBody TeacherVO t){
System.out.println("insert.t:" + t);
return 1 ;
}
/**
* 测试链接:http://localhost:8080/springmvc_demo/hello/deletetest1/111
* 控制台输出:delete.id:111
* 测试结果:1
*/
@RequestMapping(value = "/deletetest1/{id}", method = RequestMethod.DELETE)
public int deletetest1(@PathVariable String id) {
System.out.println("delete.id:" + id) ;
return 1 ;
}
/**
* 测试链接:http://localhost:8080/springmvc_demo/hello/deletetest2
* 测试header:Content-Type=application/json
* 测试body:["1","2","3"]
* 控制台输出:delete.ids:[1, 2, 3]
* 测试结果:1
*/
@RequestMapping(value = "/deletetest2", method = RequestMethod.DELETE)
@ResponseBody
public int deletetest1(@RequestBody List
ids) {
System.out.println("delete.ids:" + ids) ;
return 1 ;
}
}
(2)类TeacherVO
package com.demo.java;
public class TeacherVO {
private String id ;
private String name ;
private int age ;
public TeacherVO(){
}
public TeacherVO(String id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}