Steps to process JSON
- Add jar package
- Write the target method to return the object or collection corresponding to JSON
package com.cerr.springmvc.test;
import com.cerr.springmvc.crud.dao.EmployeeDao;
import com.cerr.springmvc.crud.entities.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Collection;
import java.util.List;
@Controller
public class SpringMVCTest1 {
@Autowired
private EmployeeDao employeeDao;
@RequestMapping(value = "/testJson")
public Collection <Employee> testJson(){
return employeeDao.getAll();
}
}
- Add a
@ResponseBody
comment on the method The added
code is as follows:
package com.cerr.springmvc.test;
import com.cerr.springmvc.crud.dao.EmployeeDao;
import com.cerr.springmvc.crud.entities.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Collection;
import java.util.List;
@Controller
public class SpringMVCTest1 {
@Autowired
private EmployeeDao employeeDao;
@ResponseBody
@RequestMapping(value = "/testJson")
public Collection <Employee> testJson(){
return employeeDao.getAll();
}
}
After the browser accesses the request, press F12 to view the returned JSON data:
Access flow between request and SpringMVC
HttpMessageConverter<T>
HttpMessageConverter<T>
It is an interface newly added by Spring 3.0, which is responsible for converting request information into an object (type T) and outputting the object (type T) as response information .
To HttpMessageConverter<T>
convert and bind the request information to the input parameters of the processing method or convert the response result into the corresponding type of response information , Spring provides two ways:
- Use
@RequestBody/@ResponseBody
to label the processing method - Use
HttpEntity<T>/ResponseEntity<T>
input parameters or return values as processing methods
When the controller processing method uses @RequestBody/@ResponseBody
or HttpEntity<T>/ResponseEntity<T>
, Spring first Accept
selects the matching HttpMessageConverter
according to the attributes of the request header or response header , and then filters the matching according to the parameter type or generic typeHttpMessageConverter
, and HttpMessageConverter
reports an error if it cannot find the available one .
Note: @RequestBody
And @ResponseBody
does not need to appear in pairs .
Example: Write an example of uploading files The
form information is as follows:
<%--
Created by IntelliJ IDEA.
User: 白菜
Date: 2019/11/16
Time: 23:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="testHttpMessageConverter" method="post" enctype="multipart/form-data">
File : <input type="file" name="file" />
Desc : <input type="text" name="desc" />
<input type="submit" value="submit" />
</form>
</body>
</html>
The processing method is as follows:
@ResponseBody
@RequestMapping("/testHttpMessageConverter")
public String testHttpMessageConverter(@RequestBody String body){
System.out.println(body);
return "helloworld" + new Date();
}
The file and description will be output on the console together, and helloworld will be output on the web side