Mainly used in type conversion date, came a front-end date, and how the back-end to receive it?
Let's look at a simple example:
Defining an interface, the method receives a parameter of type Date:
@RestController
public class UserController {
@GetMapping("/hello")
public void hello(Date date){
System.out.println(date);
}
}
Passing a value of type date when the front access:
you can see, the date type conversion request failed because of an exception, since we can define a date type converter to solve this problem:
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class DateConvert implements Converter<String, Date> {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@Override
public Date convert(String source) {
if(source != null && !"".equals(source)){
try {
Date date = sdf.parse(source);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
}
Visit again, OK!