Search code examples
javaspringspring-bootpostman

HTTP Request Returns 200 OK but No Content in Response in Spring Boot


I'm encountering an issue with my Spring Boot application where HTTP requests are returning a status of 200 OK, but the response body is empty ({}). Here's a concise summary of the problem and what I've tried so far:

Problem: When I make a GET request to /find-flights/BOM/DEL/2024-06-17, I receive the following response:

{
  "message": "Flights Found",
  "messageType": "success",
  "statusCode": "OK",
  "responseData": [{}]
}

The responseData array should contain flight information fetched from mySQL Server database, but it's empty ({}), indicating that no flights were returned despite the request being successful.

Despite these efforts, the responseData array remains empty. I'm unsure why the flights are not being retrieved from the database despite the request returning a 200 OK status.

Here is my Controller:

package com.smit_project.airline_reservation_system.controller;

import com.smit_project.airline_reservation_system.entity.Flight;
import com.smit_project.airline_reservation_system.responses.ObjectResponse;
import com.smit_project.airline_reservation_system.service.FlightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AppController {

    @Autowired
    private FlightService flightService;

    @GetMapping("/find-flights/{originAirport}/{destinationAirport}/{departureDate}")
    public ResponseEntity<ObjectResponse> findFlights(@PathVariable String originAirport, @PathVariable String destinationAirport, @PathVariable String departureDate) {
        System.out.println("Searching flights with origin: " + originAirport + ", destination: " + destinationAirport);

        ObjectResponse resp = new ObjectResponse();

        try{
            List<Flight> flights = flightService.findFlightsByAirport(originAirport, destinationAirport);
            if (flights.isEmpty()) {
                resp.setMessage("No Flights Found");
                resp.setMessageType("error");
                resp.setStatusCode(HttpStatus.OK);
            } else {
                resp.setResponseData(flights);
                resp.setStatusCode(HttpStatus.OK);
                resp.setMessage("Flights Found");
                resp.setMessageType("success");
            }
        }catch (Exception ex){
            resp.setStatusCode(HttpStatus.BAD_REQUEST);
            resp.setMessage("Error occurred on the server");
            resp.setMessageType("error");
            ex.printStackTrace();
        }

        return new ResponseEntity<>(resp, resp.getStatusCode());
    }

}

Here is the service file:

package com.smit_project.airline_reservation_system.service;

import com.smit_project.airline_reservation_system.entity.Flight;
import com.smit_project.airline_reservation_system.repository.FlightRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlightService {

    @Autowired
    private FlightRepository flightRepository;

    public void saveFlight(Flight flight) {
        flightRepository.saveAndFlush(flight);
    }

    public List<Flight> findFlightsByDate(String originAirport, String destinationAirport, String departureDate) {
        return flightRepository.findByOriginAirportAndDestinationAirportAndDepartureDate(originAirport, destinationAirport, departureDate);
    }

    public List<Flight> findFlightsByDateBetween(String startDate, String endDate) {
        return flightRepository.findByDepartureDateBetween(startDate, endDate);
    }

    public List<Flight> findFlightsByAirport(String originAirport, String destinationAirport) {
        return flightRepository.findByOriginAirportAndDestinationAirport(originAirport, destinationAirport);
    }
}

And here is the repository :

package com.smit_project.airline_reservation_system.repository;

import com.smit_project.airline_reservation_system.entity.Flight;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface FlightRepository extends JpaRepository<Flight, Integer> {

    List<Flight> findByOriginAirportAndDestinationAirportAndDepartureDate(String originAirport, String destinationAirport, String departureDate);

    List<Flight> findByDepartureDateBetween (String startDate, String endDate);

    List<Flight> findByOriginAirportAndDestinationAirport(String originAirport, String destinationAirport);
}

Here is ObjectResponse:

package com.smit_project.airline_reservation_system.responses;


import org.springframework.http.HttpStatus;

public class ObjectResponse {

    private Object responseData;
    private HttpStatus statusCode;
    private String message;
    private String messageType;

    public Object getResponseData() {
        return responseData;
    }

    public void setResponseData(Object responseData) {
        this.responseData = responseData;
    }

    public HttpStatus getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(HttpStatus statusCode) {
        this.statusCode = statusCode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessageType() {
        return messageType;
    }

    public void setMessageType(String messageType) {
        this.messageType = messageType;
    }
}


Solution

  • Empty responseData might be because of missing Getter and Setter methods in Entity class.

    I am able to replicate your issue. After adding the getter and setter methods, the response data is populated. Please note, I used Lombok to generate getters and setters using @Data annotation.

    import jakarta.persistence.*;
    import lombok.Data;
    
    @Entity(name = "Flight")
    @Table(name = "flights", catalog = "flightdb")
    @Data
    public class Flight {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "flightid")
        private Long flightId;
        @Column(name = "originairport")
        private String originAirport;
        @Column(name = "destinationairport")
        private String destinationAirport;
    }
    

    Complete code can be cloned here from GitHub.

    API Request (GET): http://localhost:8080/find-flights/BOM/DEL/2024-06-17

    API Response:

    {
        "responseData": [
            {
                "flightId": 1,
                "originAirport": "BOM",
                "destinationAirport": "DEL"
            }
        ],
        "statusCode": "OK",
        "message": "Flights Found",
        "messageType": "success"
    }