Basically I have two methods in controller that have these return types: ResponseEntity and ResponseEntity. I can't see any difference between their responses.
@GetMapping("/example/json")
public ResponseEntity<Student> exampleJson() {
Student student = Student.builder().rollNo(10).name("Student1").className("first").build();
return ResponseEntity.ok(student);
}
What will change if instead of public ResponseEntity<Student> exampleJson()
I will have ResponseEntity exampleJson()
?
EDIT
In my example, I have this method
@GetMapping("/departments/{departmentId}")
public ResponseEntity getDepartmentById(@PathVariable("departmentId") Long departmentId) {
Optional<Department> department = null;
try {
department = departmentService.getDepartmentById(departmentId);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Can't get department because there is no department with that id");
}
DepartmentDTO departmentDTO = (DepartmentDTO) modelMapper.convertToType(department.get(), DepartmentDTO.class);
return ResponseEntity.ok().body(departmentDTO);
}
Let's say that I want to have a type for my ResponseEntity, something like
public ResponseEntity<DepartmentDTO> getDepartmentById(...)
this return ResponseEntity.ok().body(departmentDTO);
will work fine but this: return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Can't get department because there is no department with that id");
will give an error as it says that
Required type: ResponseEntity<DepartmentDTO>
Provided: ResponseEntity<String>
I looked over some articles about this topic but I still don't understand