Converter 和 HttpMessageConverter的区别?

Converter 和 HttpMessageConverter的区别?两者各有什么作用?

Converter和HttpMessageConverter的区别如下:

功能不同:HttpMessageConverter是将HTTP请求内容转换成java对象,以及将java对象转换成HTTP响应内容;Converter是将一个java对象转换成另一种java对象。
使用场景不同:HttpMessageConverter主要用于HTTP主体和java对象之间的转换,Converter主要用于数据转换。
注册位置不同:HttpMessageConverter注册在HttpMessageConverterRegistry中,Converter注册在FormatterRegistry中。

Converter 是 HttpMessageConverter 的基类

  • 这篇博客: HttpMessageConverter详解中的 七、HttpMessageConverter详解 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • HttpMessageConverter,报文信息转换器。
    作用:
    1、将请求报文转化为java对象
    2、将java对象转化为响应报文

    HttpMessageConverter提供了两个注解和两个类供我们使用:@RequestBody、@ResponseBody;RequestEntity、ResponseEntity。

    下面就让我们详细来学学吧。

    首先,新建一个Moodle,然后修改pom.xml内容如下

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.example</groupId>
        <artifactId>rest_mvc</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
    
        <packaging>war</packaging>
    
        <dependencies>
            <!--SpringMVC-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.3.9</version>
            </dependency>
    
            <!--ServletAPI-->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.1</version>
                <scope>provided</scope>
            </dependency>
    
            <!--Spring5和Thymeleaf整合包-->
            <dependency>
                <groupId>org.thymeleaf</groupId>
                <artifactId>thymeleaf-spring5</artifactId>
                <version>3.0.12.RELEASE</version>
            </dependency>
        </dependencies>
    
    </project>
    

    然后写我们的springMVC.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
           https://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc
           https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <!--扫描组件-->
        <context:component-scan base-package="com.example"/>
    
        <!--配置Thymeleaf视图解析器(针对h5文件,抄就完了)-->
        <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
            <property name="order" value="1"/>
            <property name="characterEncoding" value="UTF-8"/>
            <property name="templateEngine">
                <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                    <property name="templateResolver">
                        <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                            <!--视图前缀-->
                            <property name="prefix" value="/WEB-INF/templates/"/>
                            <!--视图后缀-->
                            <property name="suffix" value=".html"/>
                            <property name="templateMode" value="HTML5"/>
                            <property name="characterEncoding" value="UTF-8"/>
                        </bean>
                    </property>
                </bean>
            </property>
        </bean>
    
        <!--
            处理静态资源,如html、js、css、jpg
            若只设置该标签,则只访问静态资源,其他请求无法访问
            此时必须设置<mvc:annotation-driven/>解决问题
        -->
        <mvc:default-servlet-handler/>
    
        <!--开启mvc注解驱动-->
        <mvc:annotation-driven>
            <mvc:message-converters>
                <!--处理响应中文乱码问题-->
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="defaultCharset" value="UTF-8"/>
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html</value>
                            <value>application/json</value>
                        </list>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>
    </beans>
    
  • 以下回答由chatgpt基于相关博客总结生成:

    Converter和HttpMessageConverter的不同点:

    • Converter是Spring框架中的一个泛型接口,用于将一个对象转换成另一个对象,通常用于数据绑定、类型转换等。Converter通常在控制器方法参数绑定的过程中调用。
    • HttpMessageConverter是用于处理http请求和响应时消息到对象的转换以及对象到消息的转换,通过它可以实现RequestBody和ResponseBody的自动转换工作,将报文中的数据自动地转换成相关的对象或者将Java对象自动地序列化为报文。HttpMessageConverter通常在拦截器或者过滤器中使用。

    它们各自的作用:

    • Converter的主要作用是将一个对象类型转换成另一种类型,常用于数据校验、数据格式化等场景,如字符串转日期类型、字符串的口令转换成加密后的口令等。
    • HttpMessageConverter的主要作用是完成HTTP请求/响应消息体和Java对象之间的转换,常用于处理RequestBody和ResponseBody的内容,如JSON数据的自动转换,XML数据的自动转换等。

    自定义HttpMessageConverter的步骤:

    • 编写自己的Converter类,需要实现HttpMessageConverter接口或其子类,重写相应的方法。
    • 编写WebConfig类,继承WebMvcConfigurerAdapter,重写其中的extendMessageConverters方法,将自定义的Converter添加到converters列表中。
    • 在自定义的Converter中添加自定义的MediaType,并修改canRead、canWrite方法,表示该Converter可以读、写指定的MediaType。
    • 在Controller的@RequestMapping中指定consumes或produces,表示需要处理的请求或返回的MediaType。