package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
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;
@Controller
@RequestMapping("/rest")
public class RestController {
//存储学生信息
private List<Student> sList = new ArrayList<Student>();
//初始化
public RestController(){
Student s1 = new Student(2017001, "张三", 23, "男", "计算机");
Student s2 = new Student(2017002, "李四", 23, "男", "计算机");
Student s3 = new Student(2017003, "王五", 23, "男", "计算机");
sList.add(s1);
sList.add(s2);
sList.add(s3);
}
//查询所有
@ResponseBody
@RequestMapping(value="/student",method=RequestMethod.GET)
public Object getAll(){
System.out.println("GET:ALL");
return sList;
}
//查询单个
@ResponseBody
@RequestMapping(value="/student/{id}",method=RequestMethod.GET)
public Object getOne(@PathVariable("id") Integer id){
System.out.println("GET:"+id);
List<Student> selectList = new ArrayList<Student>();
for(Student s : sList){
if(s.getId()==id){
selectList.add(s);
}
}
return selectList;
}
//添加
@ResponseBody
@RequestMapping(value="/student",method=RequestMethod.POST)
public Object post(@RequestBody Student student){
System.out.println("POST:"+student.getId());
sList.add(student);
return sList;
}
//修改
@ResponseBody
@RequestMapping(value="/student/{id}",method=RequestMethod.PUT)
public Object put(@PathVariable("id") Integer id,@RequestBody Student student){
System.out.println("PUT:"+id);
List<Student> removeList = new ArrayList<Student>();
for (Student s : sList) {
if(s.getId()==id){
student.setId(s.getId());
removeList.add(s);
}
}
sList.removeAll(removeList);
sList.add(student);
return sList;
}
//删除所有
@ResponseBody
@RequestMapping(value="/student",method=RequestMethod.DELETE)
public Object delete(){
System.out.println("DELETE:ALL");
sList.clear();
return sList;
}
//删除单个
@ResponseBody
@RequestMapping(value="/student/{id}",method=RequestMethod.DELETE)
public Object delete(@PathVariable("id") Integer id){
System.out.println("DELETE:"+id);
List<Student> removeList = new ArrayList<Student>();
for (Student s : sList) {
if(s.getId()==id){
removeList.add(s);
}
}
sList.removeAll(removeList);
return sList;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
springmvc+rest+json交互+接口
共32个文件
jar:11个
xml:4个
class:3个
需积分: 10 51 下载量 199 浏览量
2017-12-19
22:27:41
上传
评论
收藏 4.86MB ZIP 举报
温馨提示
实例:用springmvc框架实现rest风格的json请求和返回接口
资源推荐
资源详情
资源评论
收起资源包目录
springmvc.zip (32个子文件)
springmvc
src
springmvc-servlet.xml 2KB
test
RestController.java 3KB
Student.java 1006B
mvcController.java 2KB
.classpath 834B
build
classes
springmvc-servlet.xml 2KB
test
RestController.class 4KB
mvcController.class 3KB
Student.class 1KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 479B
.jsdtscope 503B
org.eclipse.jdt.core.prefs 364B
WebContent
META-INF
MANIFEST.MF 39B
js
jquery-3.2.1.min.js 85KB
index.jsp 341B
WEB-INF
jsp
hello.jsp 267B
lib
spring-aop-4.0.5.RELEASE.jar 344KB
spring-webmvc-4.0.5.RELEASE.jar 647KB
spring-beans-4.0.5.RELEASE.jar 654KB
jackson-databind-2.2.1.jar 845KB
commons-logging-1.1.jar 52KB
spring-core-4.0.5.RELEASE.jar 953KB
jackson-annotations-2.2.1.jar 33KB
spring-context-4.0.5.RELEASE.jar 954KB
spring-web-4.0.5.RELEASE.jar 657KB
jackson-core-2.2.1.jar 188KB
spring-expression-4.0.5.RELEASE.jar 200KB
web.xml 901B
.project 1KB
共 32 条
- 1
资源评论
-bin_bin-
- 粉丝: 34
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功