SpringBoot框架学习笔记(六):自定义转换器、内容协商 和 Thymeleaf

1 自定义转换器

1.1 基本介绍

(1)SpringBoot在响应客户端请求时,将提交的数据封装成对象时,使用了内置的转换器,一共提供了124个内置转换器,核心源码,在 GenericConverter 接口的内部类 ConvertiblePair 中

(3)可以在包 org.springframework.core.convert.support 下看到所有的124个内置转换器

(3)SpringBoot也支持自定义转换器

1.2 自定义转换器应用实例

需求说明:演示自定义转换器 String-Car

代码实现:

(1)准备如下代码

实体类 Car.java

package com.springboot.entity;

import lombok.Data;
import org.springframework.stereotype.Component;


@Data
@Component
public class Car {
    private String name;
    private Double price;
}

实体类 Monster.java,表单提交的数据直接封装到这个实体类中

package com.springboot.entity;

import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.Date;

@Data
@Component
public class Monster {
    private Integer id;
    private String name;
    private Integer age;
    private Boolean isMarried;
    private Date birth;
    private Car car;
}

前端静态页面 save.html,car 属性的封装使用自定义转换器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加妖怪</title>
</head>
<body>
<h1>添加妖怪(测试封装POJO)</h1>
<form action="/saveMonster" method="post">
    编号: <input name="id" value="100"><br/>
    姓名: <input name="name" value="牛魔王"><br/>
    年龄: <input name="age" value="500"><br/>
    婚否: <input name="isMarried" value="true"><br/>
    生日: <input name="birth" value="2000/11/11"><br/>
    <!--这里使用自定义转换器关联car,字符串整体提交,使用,号作为分隔符-->
    坐骑: <input name="car" value="奔驰,666.6"><br/>
    <input type="submit" value="保存">
</form>
</body>
</html>

后端 Controller 方法

// 添加monster
@PostMapping("/saveMonster")
public String saveMonster(Monster monster) {
    System.out.println("monster-" + monster);
    return "success";
}

(2)创建软件包 config,在该包下创建 WebConfig.java 作为配置类来配置自定义转换器

package com.springboot.config;

import com.springboot.entity.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Configuration(proxyBeanMethods = false)
 * 1. 表示 WebConfig 是一个配置类
 * 2. proxyBeanMethods = false 使用Lite模式,每个@Bean方法被调用多少次返回的组件都是单实例的。
 * 默认为true,即full模式,每个@Bean方法被调用多少次返回的组件都是新创建的
 */
@Configuration(proxyBeanMethods = false)
public class WebConfig {

    // 注入bean WebMvcConfigurer
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {

        return new WebMvcConfigurer() {
            @Override
            public void addFormatters(FormatterRegistry registry) {
                /**
                 * 1. 在 addFormatters 方法中,增加一个自定义的转换器 String -> Car
                 * 2. 增加的自定义转换器会注册到 converters 容器中
                 * 3. converters 底层结构是 ConcurrentHashMap 内置有124个转换器
                 */

                // 1. 创建自定义转换器
                Converter<String, Car> converter = new Converter<String, Car>() {

                    @Override
                    public Car convert(String source) {// source就是传入的字符串
                        // 在这里加入自定义的转换业务逻辑代码

                        // 判断是否为空
                        if (ObjectUtils.isEmpty(source)){
                            return null;
                        }

                        // 不为空就进行转换
                        Car car = new Car();
                        String[] split = source.split(",");
                        car.setName(split[0]);
                        car.setPrice(Double.valueOf(split[1]));
                        return car;
                    }
                };

                // 添加转换器到converters
                registry.addConverter(converter);
            }
        };
    }

}

小细节:转换器的key是 原类型-> 目标类型,所有如果自定义两个相同的转换器,后面加入的转换器会把前面的覆盖掉

(3)启动主程序,浏览器输入 http://localhost:8080/wwj/save.html 进行测试

 

 点击保存后控制台输出如下

2 内容协商

2.1 基本介绍

(1)根据客户端接收能力不同,SpringBoot返回不同媒体类型的数据

(2)比如:客户端Http请求 Accept: application/xml 则返回xml数据,客户端Http请求 Accept: application/json 则返回json数据

(3)如果 Accept 的值为 */*,表示所有类型的数据都接收,这时候就会按默认的格式,即JSON返回数据

2.2 应用实例

(1)创建 ResponseController.java,注意方法上需要加上@ResponseBody注解

package com.springboot.controller;

import com.springboot.entity.Car;
import com.springboot.entity.Monster;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class ResponseController {

    // 返回Monster数据-要求以json格式返回

    @GetMapping("/get/monster")
    @ResponseBody
    public Monster getMonster(){

        // 实际开发中是从DB中获取,这里模拟一个monster对象
        Monster monster = new Monster();
        monster.setId(100);
        monster.setName("地宫王");
        monster.setAge(200);
        monster.setIsMarried(false);
        monster.setBirth(new Date());
        Car car = new Car();
        car.setName("奔驰");
        car.setPrice(2222.2);
        monster.setCar(car);
        return monster;
    }
}

(2)使用postman进行测试,先测试返回json数据

(3)成功返回json数据后,再来测试一下返回xml 类型数据。

要想返回xml类型数据,需要在pom.xml文件中加入一个依赖

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

打开postman进行测试

2.3 注意事项

(1)没有加入xml依赖时,浏览器会默认返回json格式数据,当加入了xml依赖后,浏览器就会返回xml格式数据。这是为什么呢,我们看一下浏览器请求头的 Accept 字段的值就知道了

请求头的 Accept 字段的值为 text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9。

分析

  • 支持接收 html,xhtml+xml,xml,且q=0.9,表示权重为0.9
  • 支持接收 image 和 */*,且q=0.8,表示权重为0.8
  • 所以,服务器会优先返回 xml

(2)对于浏览器,我们无法修改其Accept的值,怎么办?解决方案: 开启支持基于请求参数的内容协商功能

修改application.yml, 开启基于请求参数的内容协商功能

spring:
  mvc:
    # 修改静态资源访问前缀
    static-path-pattern: /wwj/**
    hiddenmethod:
      filter:
        # 开启页面表单的 Rest 功能
        enabled: true
    # 配置视图解析器
    view:
      # 后缀
      suffix: .html
      # 前缀
      prefix: /wwj/ # 这里需要注意 prefix 需要和当前的 static-path-pattern一致
    contentnegotiation:
      favor-parameter: true # 开启基于请求参数的内容协商功能

重启服务器,打开浏览器进行测试

当我们输入 http://localhost:8080/get/monster?format=json 时,就会返回json类型数据

输入 http://localhost:8080/get/monster?format=xml 时,就会返回xml类型数据

(3)参数名format是规定好的,在开启请求参数的内容协商功能后,SpringBoot底层ParameterContentNegotiationStrategy会通过format来接收参数,然后返回对应的数据格式。可以在application.yml文件中指定参数名

spring:
  mvc:
    # 修改静态资源访问前缀
    static-path-pattern: /wwj/**
    hiddenmethod:
      filter:
        # 开启页面表单的 Rest 功能
        enabled: true
    # 配置视图解析器
    view:
      # 后缀
      suffix: .html
      # 前缀
      prefix: /wwj/ # 这里需要注意 prefix 需要和当前的 static-path-pattern一致
    contentnegotiation:
      favor-parameter: true # 开启基于请求参数的内容协商功能
      parameter-name: wwjformat # 指定接收参数名

进行测试,现在需要在浏览器输入 http://localhost:8080/get/monster?wwjformat=xml 才能返回对应类型数据格式

  • 16
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值