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);
}

New contributor

Smit Patel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật