A Converter interface was introduced in Spring3, which supports converting from one Object to another. In addition to the Converter interface, implementing the ConverterFactory interface and the GenericConverter interface can also implement our own type conversion logic.
Converter interface
First look at the definition of the Converter interface
public interface Converter<S, T> {
T convert(S source);
}
It can be seen that this interface uses generics, S represents the original type, T represents the target type, and then defines a convert method, which passes in the original type object as a parameter for conversion and returns the target type object.
Next, use the Converter interface in Spring Boot to convert the String type to Data, custom object and List<custom object> respectively.
add dependencies
add spring-boot-starter-web
dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Entity class
1. User class
public class User {
private long id;
//用户名
private String name;
//出生日期
private Date birth;
//关联用户
private User linkUser;
//喜欢的文章
private List<Article> favArticles=new ArrayList<>();
//下面省略Getter和Setter方法
2. Article class
public class Article {
//文章id
private long artId;
//文章名
private String artName;
//下面省略Getter和Setter方法
}
Configuration Type Converter
The following three classes need to be @Component
annotated, otherwise they will not take effect. And implement the interface provided by Spring org.springframework.core.convert.converter.Converter
, rewrite the convert() method in it, and write your own conversion logic in the method.
1. Define a global date converter
@Component
public class DateConvert implements Converter<String,Date> {
//日期格式
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
@Override
public Date convert(String s) {
if (s!=null&&!"".equals(s)){
try {
//解析参数
Date date=sdf.parse(s);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
}
2. Define the global object converter
Here, the readValue() function of Jackson's ObjectMapper class is used to deserialize the Json string into a Java object
@Component
public class ObjectConvert implements Converter<String,User> {
@Override
public User convert(String s) {
ObjectMapper objectMapper=new ObjectMapper();
if (s!=null&&!"".equals(s)){
try {
User user=objectMapper.readValue(s,User.class);
return user;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
return null;
}
}
3. Define a global List type converter
Here, the readValue() function of Jackson's ObjectMapper class is used to deserialize the Json string into a List
@Component
public class StringToListController implements Converter<String, List<Article>> {
ObjectMapper objectMapper=new ObjectMapper();
@Override
public List<Article> convert(String s) {
List<Article> list=null;
try {
list=objectMapper.readValue(s, new TypeReference<List<Article>>() {
});
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return list;
}
}
controller
Note here that using produces sets the type of returned data to json, and consumes sets the type of submitted content to: application/x-www-form-urlencoded
.
application/x-www-form-urlencoded
Function: Connect the parameters of the key-value pair with &, if there is a space, convert the space to a plus sign; if there is a special symbol, convert the special symbol to an ASCII HEX value.
@RestController
public class HelloController {
@GetMapping("hello")
public Date getDate(Date birth){
System.out.println(birth);
return birth;
}
@PostMapping(value="showUser",produces="application/json",
consumes = "application/x-www-form-urlencoded")
public User showUser(User user){
return user;
}
}
test
Test in Postman, pay attention to the following settings: POST request -> Body -> x-www-form-urlencoded. Enter parameters in Body for testing.
Because there are Json type parameters in the parameters, if you directly use Params to send data, a request parameter exception error will occur.
Test Results: