Reputation: 11
I have an Operation service that has a list function that returns a list of OperationDTO however when the controller calls this function the list returns null dto objects and if I change the type of return in the service to Operation entity type the controller works fine
public class OperationDTO {
private Long user_id;
private Long account_id;
private int amount;
private TypeOperation typeOperation;
private boolean status;
private Date created;
private Date updated;
public OperationDTO(Operation operation){
this.user_id = operation.getUser().getId();
this.account_id = operation.getAccount().getId();
this.amount = operation.getAmount();
this.typeOperation = operation.getTypeOperation();
this.status = operation.isStatus();
this.created = operation.getCreated();
this.updated = operation.getUpdated();
}
public OperationDTO(){}
@Override
public String toString() {
return this.user_id + " "+
this.account_id + " "+
this.typeOperation + " "+
this.amount+ " "+
this.status+ " "+
this.created+ " "+
this.updated
;
}
}
this is my implementation to my service
public Collection<OperationDTO> listOperation() {
Collection<Operation> operations = (Collection<Operation>)
operationRepository.findAll();
Collection<OperationDTO> operationDTOS = new ArrayList<>();
for (Operation oper: operations){
OperationDTO op = new OperationDTO(oper);
operationDTOS.add(op);
}
System.out.println("Les operations " + operationDTOS);
return operationDTOS;
}
My controller
public ResponseEntity<EntityResponse> list(){
return ResponseEntity.ok(
EntityResponse.builder()
.timeStamp(LocalDateTime.now())
.message("List of Operations" )
.status(HttpStatus.OK)
.statusCode(HttpStatus.OK.value())
.data(Map.of("Operation list", operationService.listOperation()))
.build()
);
}
When a call this list in my controller with postman I have this response
{
"timeStamp": [
2022,
5,
13,
20,
46,
57,
754770000
],
"statusCode": 200,
"status": "OK",
"message": "List of Operations",
"data": {
"Operation list": [
{},
{},
{}
]
}
}
please need some help
Upvotes: 0
Views: 1959
Reputation: 1
As the existing comments already stated, your OperationDTO does not provide any means of access from outside, without changes everything is just private
.
You can either annotate the fields that you want in your response with @JsonProperty
from Jackson or you can provide public getter methods, for example with Lombok. The same goes for TypeOperation.
With JsonProperty:
public class OperationDTO {
@JsonProperty("userId") // you can rename it here to camel-case, if you like
private Long user_id;
...
}
With Lombok:
@Getter
public class OperationDTO {
private Long user_id;
...
}
Witout annotations:
public class OperationDTO {
private Long user_id;
...
private Long getUserId() {
return user_id;
}
}
Notice that in the last case your value will be serialized by getter name, so you will see "userId": 1234534
in the JSON, not UserId
nor user_id
.
To get rid of boilerplate code, you can also combine both @JsonProperty
and @Getter
for the best of both worlds. When you use Lombok for the first time, please read how to set up the annotation processing in your IDE.
Also note that this answer only provides hints on serialization, if you ever want to read a JSON, there are also other things missing which I won't cover here.
Upvotes: 2