1

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

Leobejj
  • 41
  • 6
  • 2
    Does this answer your question? [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Sam Dec 02 '22 at 09:49
  • Kind of, now I know how a raw type works and the difference between it and generic type. I will make an edit to explain better my problem – Leobejj Dec 02 '22 at 10:00

1 Answers1

0

You can define what type of object your API will return. Basically you're defining a contract for this API.

z48o0
  • 98
  • 7