Spring MVC-HttpMessageConverter


目录
  • 概括
  • 环境
  • @RequestBody
  • RequestEntity
  • @ResponseBody
    • 返回普通类型
    • 返回json
  • @RestController
  • ResponseEntity

概括

HttpMessageConverter,报文信息转换器,用于在HTTP请求和响应之间进行转换的策略接口,即将请求报文转换为Java对象,或将Java对象转换为响应报文。
HttpMessageConverter提供了两个注解和两个类型:@RequestBody,@ResponseBody,RequestEntity,ResponseEntity

环境


        
        
            org.springframework
            spring-webmvc
            5.3.1
        
        
        
            ch.qos.logback
            logback-classic
            1.2.3
        
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        
        
        
            org.thymeleaf
            thymeleaf-spring5
            3.0.12.RELEASE
        

    
  
    

    
    
        
        
        
            
                
                    
                        
                        
                        
                        
                        
                        
                    
                
            
        
    

    
    

    

        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceResponseEncoding
            true
        
    

    
        CharacterEncodingFilter
        /*
    

    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springMVC.xml
        
        1
    

    
        DispatcherServlet
        /
    



  
  首页





@RequestBody

获取请求体,在控制器方法中用@RequestBody标识一个形参,前请求的请求体就会为当前注解所标识的形参赋值。


用户名:
密码:
package com.fly.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author 26414
 */
@Controller
public class HttpController {

  @RequestMapping("/testRequestBody")
  public String testRequestBody(@RequestBody String requestBody) {
    System.out.println("requestBody = " + requestBody);
    return "success";
  }

}




  
  success



success

RequestEntity

RequestEntity是封装请求报文的一种类型,需要在控制器方法的形参中设置该类型的形参,当前请求的请求报文就会赋值给该形参

  /**
   *HttpController
   */
  @RequestMapping("/testRequestEntity")
  public String testRequestEntity(RequestEntity requestEntity) {
    System.out.println("requestEntity.getHeaders() = " + requestEntity.getHeaders());
    System.out.println("requestEntity.getBody() = " + requestEntity.getBody());
    return "success";
  }

用户名:
密码:

@ResponseBody

@ResponseBody用于标识一个控制器方法,可以将该方法的返回值直接作为响应报文的响应体响应到浏览器

返回普通类型

/**
   *HttpController
   */
  @RequestMapping("/testResponseBody")
  @ResponseBody
  public String testResponseBody() {
    return "hhh";
  }

testResponseBody

返回json

1:导入jackson的依赖


    com.fasterxml.jackson.core
    jackson-databind
    2.12.1

2:在SpringMVC的核心配置文件中开启mvc的注解驱动

3:使用@ResponseBody注解
4:将Java对象直接作为控制器方法的返回值返回

package com.fly.entity;

/**
 * @author 26414
 */
public class Person {
  
  private Integer id;

  public Person() {
  }

  public Person(Integer id, String name, Integer age) {
    this.id = id;
    this.name = name;
    this.age = age;
  }

  private String name;
  private Integer age;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }
}

  /**
   *HttpController
   */
  @RequestMapping("/testResponseBodyJson")
  @ResponseBody
  public Person testResponseBodyJson() {
    return new Person(1, "张三", 12);
  }

testResponseBodyJson

@RestController

一个方便的注释,它本身带有@Controller和@ResponseBody注释。 携带该注释的类型被视为控制器,其中@RequestMapping方法默认假设@ResponseBody语义。

ResponseEntity

ResponseEntity用于控制器方法的返回值类型,该控制器方法的返回值就是响应到浏览器的响应报文