I have this simple post request:
@PostMapping
public <T> void create(T data) {
System.out.println(data);
}
Whenever I call this request (providing any data type in the form-data like a string or a number), it prints this in the console:
java.lang.Object@46093a82
it doesn't give any error, just printing this object instead of "name" for example.
What's the problem?
CodePudding user response:
java.lang.Object@46093a82
is printed, because that's what needs to be printed.
All Java objects have a toString() method (This method is defined in the Object class), which is invoked when you try to print the object.
The Object.toString() method returns a string, composed of the name of the class, an @ symbol and the hashcode of the object in hexadecimal.
To print something different when you call System.out.println(Object), you must override the toString() method in your own class.
simple example:
public class Foo {
private String name;
@Override
public String toString() {
return name;
}
}