springboot Generate tostring
时间: 2023-10-13 21:20:10 浏览: 38
In Spring Boot, you can use the Lombok library to generate the `toString()` method for your classes. Here are the steps to do so:
1. Add the Lombok dependency to your `pom.xml` file:
```xml
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
```
2. Annotate your class with `@ToString`:
```java
import lombok.ToString;
@ToString
public class MyClass {
// class fields and methods
}
```
3. That's it! Now the `toString()` method will be generated for your class with all its fields.
```java
MyClass myObject = new MyClass();
System.out.println(myObject.toString());
```
The output will be something like this:
```
MyClass(field1=value1, field2=value2, ...)
```
Note: You can also customize the `toString()` method generated by Lombok by adding parameters to the `@ToString` annotation, such as `exclude` to exclude certain fields, `callSuper` to include the superclass's `toString()` method, and more.
相关推荐
![groovy](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![groovy](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)