Table of contents
Step 1: Write a method to receive date data
Step 2: Start the Tomcat server
Step 3: Send the request using PostMan
Step 5: Change the date format
We have dealt with simple data types, POJO data types, array and collection data types, and JSON data types before. Next, we have to deal with a more common data type in development.日期类型
The date type is special, because there are more than N input methods for the date format, such as:
-
2088-08-18
-
2088/08/18
-
08/18/2088
-
......
For so many date formats, how should SpringMVC receive them, and can it handle date type data well?
Step 1: Write a method to receive date data
Add a method in the UserController class and set the parameter to a date type
@RequestMapping("/dataParam")
@ResponseBody
public String dataParam(Date date)
System.out.println("参数传递 date ==> "+date);
return "{'module':'data param'}";
}
Step 2: Start the Tomcat server
Check the console to see if there is an error. If there is an error, solve the error first.
Step 3: Send the request using PostMan
Use PostMan to send a GET request and set the date parameter
Step 4: View the console
By printing, we found that SpringMVC can receive the date data type and print it on the console.
At this time, we wondered if the format of the date parameter was changed to something else, would SpringMVC still be able to handle it?
Step 5: Change the date format
In order to better see the results of the program running, we add an additional date parameter to the method
@RequestMapping("/dataParam")
@ResponseBody
public String dataParam(Date date,Date date1)
System.out.println("参数传递 date ==> "+date);
return "{'module':'data param'}";
}
Use PostMan to send the request, carrying two different date formats,
http://localhost/dataParam?date=2088/08/08&date1=2088-08-08
After sending the request and data, the page will report 400, and the console will report an error
Resolved [org.springframework.web.method.annotation.==MethodArgumentTypeMismatchException==: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.==ConversionFailedException==: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2088-08-08'; nested exception is java.lang.IllegalArgumentException]
As can be seen from the error message, the reason for the error is that it 2088-08-08
failed when converting to a date type. The reason is that the string to date format supported by SpringMVC by default is yyyy/MM/dd
, and what we are passing now does not conform to its default format, so SpringMVC cannot Format conversion is performed, so an error is reported.
The solution is also relatively simple, requiring the use of@DateTimeFormat
@RequestMapping("/dataParam")
@ResponseBody
public String dataParam(Date date,
@DateTimeFormat(pattern="yyyy-MM-dd") Date date1)
System.out.println("参数传递 date ==> "+date);
System.out.println("参数传递 date1(yyyy-MM-dd) ==> "+date1);
return "{'module':'data param'}";
}
Restart the server, resend the request test, and SpringMVC can correctly convert the date
Step 6: Date Carrying Time
Next, let's send a date with time to see how SpringMVC handles it?
First modify the UserController class and add a third parameter
@RequestMapping("/dataParam")
@ResponseBody
public String dataParam(Date date,
@DateTimeFormat(pattern="yyyy-MM-dd") Date date1,
@DateTimeFormat(pattern="yyyy/MM/dd HH:mm:ss") Date date2)
System.out.println("参数传递 date ==> "+date);
System.out.println("参数传递 date1(yyyy-MM-dd) ==> "+date1);
System.out.println("参数传递 date2(yyyy/MM/dd HH:mm:ss) ==> "+date2);
return "{'module':'data param'}";
}
Use PostMan to send the request, carrying two different date formats,
http://localhost/dataParam?date=2088/08/08&date1=2088-08-08&date2=2088/08/08 8:08:08
Restart the server, resend the request test, and SpringMVC can convert the date and time data
Knowledge point 1: @DateTimeFormat
name | @DateTimeFormat |
---|---|
type | ==Formal parameter annotation== |
Location | Before the SpringMVC controller method formal parameters |
effect | Set date and time data format |
related attributes | pattern: Specifies the date and time format string |
Internal realization principle
Before explaining the internal principles, we need to think about a question:
-
The front end passes the string, and the back end uses the date to receive it
-
The front end passes JSON data, and the back end uses objects to receive
-
There are many data types needed in the background
-
The front end passes strings, and the back end uses Integer to receive
-
There are many types of conversions in the data transfer process
Q: Who will do this type conversion?
Answer: Spring MVC
Q: How does SpringMVC implement type conversion?
Answer: Converter interface
In the framework, there is a type conversion interface
-
Converter interface
/**
* S: the source type
* T: the target type
*/
public interface Converter<S, T> {
@Nullable
//该方法就是将从页面上接收的数据(S)转换成我们想要的数据类型(T)返回
T convert(S source);
}
Note: The package to which Converter belongs isorg.springframework.core.convert.converter
-
Implementation class of Converter interface
The framework provides many implementation classes corresponding to the Converter interface, which are used to realize the conversion between different data types. We have actually been using this type conversion before, such as:
-
Request parameter age data (String→Integer)
-
Convert json data to object (json → POJO)
-
Date format conversion (String → Date)
== Note: The configuration class of SpringMVC uses @EnableWebMvc as a standard configuration, do not omit ==