Using DTO layer in Spring Boot development
In the development process of Spring Boot, it is a very common practice to use the DTO (Data Transfer Object) layer. The DTO layer is an intermediate layer introduced between the business logic layer and the data access layer of the application, and is used to transfer data between different layers. This article will introduce the basic syntax of the DTO layer and why it is necessary to use the DTO layer in Spring Boot development, and provide actual case code.
Why do you need a DTO layer?
In Spring Boot development, there are several important reasons for using the DTO layer:
1. Data encapsulation
The DTO layer can encapsulate the data of multiple entity classes into a DTO object, making data transmission more convenient. DTO objects usually only contain data fields that need to be transferred, not business logic. This reduces the size of the data transfer and improves performance.
2. Decoupling
The DTO layer can decouple the business logic layer and the data access layer. The business logic layer can interact with the data access layer through the DTO object without directly operating the database entity class. This can reduce code coupling and improve code maintainability and testability.
3. Data format conversion
The DTO layer can perform data format conversion. For example, convert the data of the database entity class into the JSON format data required by the front end, or convert the JSON data passed by the front end into the format required by the database entity class. This can simplify the code logic of data conversion and improve development efficiency.
4. Security
The DTO layer can be used to filter sensitive data. During data transmission, sensitive data that does not need to be transmitted can be filtered out through the DTO layer to protect data security.
sample code
- Better Encapsulation of Data
First, define a DTO class to encapsulate the data fields that need to be transmitted:
public class UserDto {
private String username;
private String email;
// 省略构造方法和getter/setter方法
}
Then, use DTO objects in the business logic layer for data transfer:
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public UserDto getUserById(Long id) {
User user = userRepository.findById(id);
// 将User对象转换成UserDto对象
UserDto userDto = new UserDto();
userDto.setUsername(user.getUsername());
userDto.setEmail(user.getEmail());
return userDto;
}
// 省略其他业务方法
}
In the above code, getUserById
the method gets an User
object from the database, and then converts it into UserDto
an object containing only the data fields that need to be transferred.
Finally, use DTO objects in the controller layer for data transfer:
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public UserDto getUserById(@PathVariable Long id) {
// 调用业务逻辑层的方法获取UserDto对象
UserDto userDto = userService.getUserById(id);
return userDto;
}
// 省略其他控制器方法
}
In the above code, getUserById
the method calls the method of the business logic layer to obtain an UserDto
object, and then returns it to the front end as a response.
By using the DTO layer, data encapsulation, decoupling, data format conversion and data security can be improved. This makes the code clearer, maintainable and testable.
- Prevent triggering no-argument constructors
When the class is passed directly User
, the no-argument constructor is triggered. This is because in Spring Boot, the default is to use a no-argument constructor to create an object, and then set the properties of the object through reflection.
User
Here's an example code that demonstrates the no-argument constructor being triggered when the class is passed directly :
public class User {
private String username;
private String email;
public User() {
System.out.println("User constructor called");
}
// 省略其他构造方法和getter/setter方法
}
@RestController
@RequestMapping("/users")
public class UserController {
@PostMapping("/")
public void createUser(@RequestBody User user) {
// 创建User对象时会触发无参构造函数
System.out.println("User created: " + user.getUsername());
}
}
In the above code, User
the class defines a no-argument constructor and outputs a log in the constructor. Methods UserController
in the class use annotations to convert the JSON data in the request body into objects. When a request arrives, Spring Boot will automatically create an object and trigger the no-argument constructor.createUser
@RequestBody
User
User
When we send a POST request to /users/
the interface, and pass the following JSON data:
{
"username": "john",
"email": "[email protected]"
}
The console will output the following logs:
User constructor called
User created: john
As you can see, when the class is passed directly User
, the no-argument constructor is triggered and an User
object is created.
It should be noted that if User
other constructors are defined in the class, Spring Boot will select the appropriate constructor to create the object according to certain rules. For example, if a parameterized constructor is defined and no parameterless constructor is defined, the parameterized constructor is triggered when the object is created.
To sum up, when User
the class is passed directly, the no-argument constructor is triggered. This is because Spring Boot uses a no-argument constructor by default to create objects. If you need to use other constructors, you can define and configure them according to your specific needs.