文章目录
- 前言
- 一、DateFormatter
- 二、NumberFormat
- 1. NumberBean
- 2. number.jsp
- 三、ConverterController
- 四、执行结果
- 总结
前言
【第6章】spring类型转换器
此章节内容为spring类型转换器内容扩展,使用spring提供的注解增强转换器功能,让date和int等类型转换更加方便。
一、DateFormatter
package org.example.springmvc.converters;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
/**
* Create by zjg on 2024/5/1
*/
@Getter
@Setter
@ToString
public class DateBean {
private Date date1;
@DateTimeFormat(pattern = "yyyyMMdd")
private Date date2;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date date3;
@DateTimeFormat(pattern = "yyyy/MM/dd")
private Date date4;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date date5;
@DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss")
private Date date6;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate date7;
@DateTimeFormat(pattern = "HH:mm:ss")
private LocalTime date8;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime date9;
}
二、NumberFormat
1. NumberBean
package org.example.springmvc.converters;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.format.annotation.NumberFormat;
import java.math.BigDecimal;
import java.math.BigInteger;
/**
* Create by zjg on 2024/5/1
*/
@Getter
@Setter
@ToString
public class NumberBean {
@NumberFormat
private short number1;
@NumberFormat
private int number2;
@NumberFormat
private long number3;
@NumberFormat(style= NumberFormat.Style.NUMBER)
private float number4;
@NumberFormat(style= NumberFormat.Style.PERCENT)
private double number5;
@NumberFormat(style= NumberFormat.Style.CURRENCY)
private BigDecimal number6;
@NumberFormat(pattern = "#,###,###,###,###")
private BigInteger number7;
}
2. number.jsp
<%--
Created by IntelliJ IDEA.
User: 张军国001
Date: 2024/5/2
Time: 12:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
<title>numberBean</title>
</head>
<body>
<spring:eval expression="numberBean.number1"/>
<spring:eval expression="numberBean.number2"/>
<spring:eval expression="numberBean.number3"/>
<spring:eval expression="numberBean.number4"/>
<spring:eval expression="numberBean.number5"/>
<spring:eval expression="numberBean.number6"/>
<spring:eval expression="numberBean.number7"/>
</body>
</html>
三、ConverterController
package org.example.springmvc.converters;
import com.alibaba.fastjson2.JSONObject;
import org.example.springmvc.converters.custom.Request;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Create by zjg on 2024/5/1
*/
@Controller
public class ConverterController {
@RequestMapping("/dateBean")
@ResponseBody
public DateBean dateBean(DateBean dateBean){
System.out.println(dateBean);
return dateBean;
}
@RequestMapping("/numberBean")
public String numberBean(NumberBean numberBean){
System.out.println(numberBean);
return "params/number";
}
}
四、执行结果
DateBean(date1=Wed May 01 22:28:20 CST 2024, date2=Wed May 01 00:00:00 CST 2024, date3=Wed May 01 00:00:00 CST 2024, date4=Wed May 01 00:00:00 CST 2024, date5=Wed May 01 22:28:20 CST 2024, date6=Wed May 01 22:28:20 CST 2024, date7=2024-05-01, date8=22:28:20, date9=2024-05-01T22:28:20)
NumberBean(number1=1, number2=1, number3=1, number4=1.123, number5=1.456, number6=1.78, number7=1000000000000)
总结
回到顶部
官方文档
postman测试脚本已上传,下载导入即可用。