SpringMVC study notes | return JSON data, HttpMessageConverter<T>

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 @ResponseBodycomment 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:

 

 
13424350-92737b7f67e4361b.png
 

 

Access flow between request and SpringMVC

 
13424350-1c522c2cbce5ec6c.png
 

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/@ResponseBodyto label the processing method
  • Use HttpEntity<T>/ResponseEntity<T>input parameters or return values ​​as processing methods

When the controller processing method uses @RequestBody/@ResponseBodyor HttpEntity<T>/ResponseEntity<T>, Spring first Acceptselects the matching HttpMessageConverteraccording to the attributes of the request header or response header , and then filters the matching according to the parameter type or generic typeHttpMessageConverter , and HttpMessageConverterreports an error if it cannot find the available one .

Note: @RequestBodyAnd @ResponseBodydoes 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

 

 
13424350-8f200d4cadd973b2.png
 

 

 
13424350-27dc6a9792f1a968.png
 

Guess you like

Origin blog.csdn.net/qq_14810195/article/details/103161209