SpringMVC4.0实现rest风格接口,json格式请求和返回
2017-12-19 22:13
656 查看
1.项目结构和引入的包:
2.web.xml:
3.springmvc-servlet.xml:
4.实体类Student.java:
5.RestController.java:
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
2.web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>springmvc</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3.springmvc-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <!-- scan the package and the sub package --> <context:component-scan base-package="test"/> <!-- don't handle the static resource --> <mvc:default-servlet-handler /> <!-- if you use annotation you must configure following setting --> <mvc:annotation-driven /> <!-- configure the InternalResourceViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean> <!-- json return --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean> </list> </property> </bean> </beans>
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; } }
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<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; } }
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
相关文章推荐
- asp.net mvc实现rest风格返回json
- 使用getJSON()异步请求服务器返回json格式数据的实现
- vs2015开发webserver 返回json格式 实现图书编目数据接口
- 解决spring boot中rest接口404,500等错误返回统一的json格式
- 请求第三方接口返回json格式数据的解析
- ssm实现crud_使用rest风格的uri、服务端返回json数据(3)
- asp.net mvc实现rest风格返回json
- rest接口返回是比较灵活的,可以根据请求者的需求返回xml,json,string等格式
- 解决spring boot中rest接口404,500等错误返回统一的json格式
- 接着上篇继续搭建restfull风格的webservice并返回json格式的值
- 中国天气网接口 返回json格式分析说明
- 中国天气网接口返回json格式分析及接口(XML、图片接口)说明!
- android利用httpclient实现post、get请求restful接口进行json和form表单数据提交等公共方法类
- Spring MVC 全局异常处理-RESTAPI接口返回统一JSON格式-自定义异常处理--404异常捕捉
- ssm框架整合及Rest风格接口实现
- SpringMVC 用JackJson做接口返回JSON格式指定的字段解决方法
- ajax请求返回的Json格式数据循环输出
- JSON API免费接口 各种提供JSON格式数据返回服务网站的API接口
- 返回map形式json数据格式接口
- 明确出需求 然后开会评审 要什么接口 接口参数、返回json内容、格式 协定好 在做