您的位置:首页 > 编程语言 > Java开发

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

2017-12-19 22:13 656 查看
1.项目结构和引入的包:



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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: