文章目录
RESTfull服务
我们可以通过以下两种方式提供RESTFull的服务
第一种方法
用@RestController注解替换Controller类上的@Controller注解,这样该类里面所有的RequestMapping方法都会强制输出返回的内容本身给调用方
第二种方法
在需要提供RESTFull的RequestMapping方法上添加@ResonpseBody注解,这样会强制此方法输出返回的内容本身给调用方。
补充
此外,SpringMVC还给我们提供了专用的RESTFull相关注解GetMapping、PostMapping、PutMapping、DeleteMapping、PatchMapping用于简化@RequestMapping注解, 比如@GetMapping注解就相当于@RequestMapping(method=RequestMethod.GET)
返回JSON数据
第一种方法
SpringMVC默认使用Jackson将我们返回的对象类型转换成JSON格式数据,我们只需要做好以下两步:
1、添加Jackson依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
2、在dispatcher-servlet.xml中开启MVC注解支持
<mvc:annotation-driven />
第二种方法
SpringMVC还可以使用fastjson将我们返回的对象类型转换成JSON格式数据,我们只需要做好以下两步:
1、添加fastjson依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.57</version>
</dependency>
2、在dispatcher-servlet.xml中开启MVC注解支持及添加配置
<mvc:annotation-driven>
<mvc:message-converters>
<!-- 设置转换JSON的参数 -->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="defaultCharset" value="UTF-8" />
<property name="fastJsonConfig">
<!-- 格式化日期时间 -->
<bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
<property name="dateFormat" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
<property name="supportedMediaTypes">
<list>
<!-- 添加text/html这种mime类型,主要是解决老版IE浏览器json数据弹出下载框问题 -->
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
RESTfull服务和返回JSON数据示例
RESTfull服务的第一种方法和手工转换JSON
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>test</title>
</head>
<body>
<a href="query_stu_shou/23">手工转换JSON</a>
</body>
</html>
StudentService .java
@Service
public class StudentService {
public Student queryStudentById(Integer id) {
Student student = new Student();
student.setId(id);
student.setSname("张三");
student.setNickName("何老大");
student.setGender("男");
student.setAge(88);
return student;
}
}
JsonController .java
@Controller
public class JsonController {
@Autowired
private StudentService studentService;
/**
* 手工转换JSON
* @param id
* @return
*/
@ResponseBody // 将响应内容直接写入响应体中,而不是通过viewResolver去找视图
@RequestMapping("/query_stu_shou/{id}")
public String queryStudent1(@PathVariable Integer id) {
Student student = studentService.queryStudentById(id);
return JSON.toJSONString(student);
}
}
RESTfull服务的第二种方法和返回JSON数据的第一种方法
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>test</title>
</head>
<body>
<a href="query_stu/23">自动转换JSON</a>
</body>
</html>
StudentService .java
@Service
public class StudentService {
public Student queryStudentById(Integer id) {
Student student = new Student();
student.setId(id);
student.setSname("张三");
student.setNickName("何老大");
student.setGender("男");
student.setAge(88);
return student;
}
}
JsonController.java
@RestController //相当于每个方法上都加了@ResponseBody
public class JsonController {
@Autowired
private StudentService studentService;
/**
* 通过全局配置(MessageConverter),自动转换JSON
* @param id
* @return
*/
@RequestMapping("/query_stu/{id}")
public Student queryStudent(@PathVariable Integer id) {
return studentService.queryStudentById(id);
}
RESTfull服务的第二种方法和返回JSON数据的第二种方法
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>test</title>
</head>
<body>
<a href="query_stus">自动转换JSON</a>
</body>
</html>
StudentService .java
@Service
public class StudentService {
public List<Student> queryStudentList() {
List<Student> students = new ArrayList<>();
for(int i = 0; i < 5; i++) {
Student student = new Student();
student.setId(i+1);
student.setSname("张三" + (i + 1));
student.setNickName("何老大" + (i + 1));
student.setGender("男");
student.setAge(i+1);
students.add(student);
}
return students;
}
}
JsonController.java
@RestController //相当于每个方法上都加了@ResponseBody
public class JsonController {
@Autowired
private StudentService studentService;
@RequestMapping("/query_stus")
public List<Student> queryStudentList() {
return studentService.queryStudentList();
}