Everything instead of Object
return me compile error in code.
If you need some more details I can provide you without any problem.
My DTO
is:
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(NON_NULL)
public class ProjectDTO implements Serializable {
public ProjectDTO(Project project) {
this.name = project.getName();
this.abbreviation = project.getAbbreviation();
this.customer = project.getCustomer();
}
private String name;
private String abbreviation;
private String customer;
}
UPD:
if I use ProjectDTO
as a type on the left side I'm receiving also a compile error as:
if I use Object
, I don't have any issues, but can I narrow the type of the method below?
@PutMapping
@Operation(summary = "Updating project")
public ResponseEntity<Object> updateById(@RequestBody Project project) {
logger.info("updateById() is calling...");
if (project != null) {
projectRepository.save(project);
return new ResponseEntity<>("Updated successfully.", HttpStatus.OK);
}
return new ResponseEntity<>("Update failed.", HttpStatus.BAD_REQUEST);
}
Thank you in advance for your attention and smart ideas.
I appreciate any help with it.
CodePudding user response:
As comments point out, you have a method returning ResponseEntity<ProjectDTO>
but you are returning ResponseEntity<String>
The actual answer will depend on the architecture of your service:
Do you want the caller to know how the updated entity is? Then
@PutMapping
@Operation(summary = "Updating project")
public ResponseEntity<ProjectDTO> updateById(@RequestBody Project project) {
logger.info("updateById() is calling...");
if (project != null) {
ProjectDTO entity = projectRepository.save(project);
return new ResponseEntity<>(entity, HttpStatus.OK);
}
return new ResponseEntity<>(new ProjectDTO("","",""), HttpStatus.BAD_REQUEST);
}
with the above code, when the update is successful it will return a populated object, and an empty one in case of failure. If you want to show your string as in your code snippet, I'd recommend you do so in the service consuming from this API.
On the other hand, if you don't care about knowing the entity and instead wish to send back a message, then
@PutMapping
@Operation(summary = "Updating project")
public ResponseEntity<String> updateById(@RequestBody Project project) {
logger.info("updateById() is calling...");
if (project != null) {
projectRepository.save(project);
return new ResponseEntity<>("Updated successfully.", HttpStatus.OK);
}
return new ResponseEntity<>("Update failed.", HttpStatus.BAD_REQUEST);
}
Take notice that I don't tell the created ResponseEntity
-ies the type. That is because you already have it defined in the methods' signature. Actually, quality assurance tools like SonarQube will complain about it, not because is wrong, but because is not neccesary.
CodePudding user response:
Your method return type is ResponseEntity<ProjectDTO>
and this is the type that you need to construct.
The return statement is therefore
ProjectDTO someProjectDTO = createProjectDTOFromProject(project);
return new ResponseEntity<ProjectDTO>(someProjectDTO, HttpStatus.OK);
Or even shorter (since Java can correctly infer the type arguments):
ProjectDTO someProjectDTO = createProjectDTOFromProject(project);
return new ResponseEntity<>(someProjectDTO, HttpStatus.OK);
The first line is some pseudo code because I don't know how you create your ProjectDTO instances.