SpringMVC4.0实现rest风格接口,json格式请求和返回

1.项目结构和引入的包: 


2.web.xml:



  springmvc
 
    index.jsp
 

 
    springmvc
    org.springframework.web.servlet.DispatcherServlet
   
      contextConfigLocation
      classpath:springmvc-servlet.xml
   

    1
 

 
    springmvc
    /
 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
3.springmvc-servlet.xml:


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                    

   
   

   
   

   
   

   
                id="internalResourceViewResolver">
       
       
       
       
   

   
     
         
             
                 
           
 
       
 
   
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
4.实体类Student.java:

package test;

public class Student {

    private int id;
    private String name;
    private int age;
    private String sex;
    private String major;


    public Student() {

    }

    //此方法会将默认的无参构造方法给覆盖掉,必须加上上面的无参构造方法
    public Student(int id, String name, int age, String sex, String major) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.major = major;
    }

    public int getId() {
        return id;
    }
    public void setId(int 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;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getMajor() {
        return major;
    }
    public void setMajor(String major) {
        this.major = major;
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
5.RestController.java:

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 sList = new ArrayList();

    //初始化
    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 selectList = new ArrayList();
        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 removeList = new ArrayList();
        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 removeList = new ArrayList();
        for (Student s : sList) {
            if(s.getId()==id){
                removeList.add(s);  
            }
        }
        sList.removeAll(removeList);
        return sList;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
6.利用postman测试接口(本例子是仿造学生表的增删改查): 
(1)GET查询所有:localhost:8080/springmvc/rest/student 

(2)GET查询单个:localhost:8080/springmvc/rest/student/2017001 

(3)POST提交单个(先设置好类型和json数据):localhost:8080/springmvc/rest/student 

(4)PUT修改单个(先设置好类型和json数据):localhost:8080/springmvc/rest/student/2017004 

(5)DELETE删除单个:localhost:8080/springmvc/rest/student/2017001 

(5)DELETE删除所有:localhost:8080/springmvc/rest/student 

源码: 
http://download.csdn.net/download/m0_37459945/10165177 
https://github.com/15629168655/springmvc-rest-json 
参考: 
http://blog.csdn.net/xinyuan_java/article/details/46696703 
http://blog.csdn.net/w605283073/article/details/51338765 
https://www.cnblogs.com/cnblog-long/p/6547380.html 
http://blog.csdn.net/lutinghuan/article/details/46820023 
http://blog.csdn.net/lzj3462144/article/details/76980779?foxhandler=RssReadRenderProcessHandler
--------------------- 
作者:-bin_bin- 
来源:CSDN 
原文:https://blog.csdn.net/m0_37459945/article/details/78848023 
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(Java)