Spring Boot 中 HTTP 请求返回 200 OK 但响应中没有内容

问题描述 投票:0回答:1

我的 Spring Boot 应用程序遇到问题,其中 HTTP 请求返回状态 200 OK,但响应正文为空 ({})。这是问题以及我迄今为止所尝试的内容的简要总结:

问题: 当我向

/find-flights/BOM/DEL/2024-06-17
发出 GET 请求时,我收到以下响应:

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

responseData 数组应包含从 mySQL Server 数据库获取的航班信息,但它是空的 ({}),表明尽管请求成功,但没有返回任何航班。

尽管做出了这些努力,responseData 数组仍然为空。我不确定为什么尽管请求返回 200 OK 状态,但没有从数据库中检索航班。

这是我的控制器:

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

}

这是服务文件:

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

这是存储库:

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

这是对象响应:

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

java spring spring-boot postman
1个回答
0
投票

responseData 可能是因为 Entity 类中缺少 GetterSetter 方法。

我能够复制您的问题。添加 getter 和 setter 方法后,将填充响应数据。请注意,我使用 Lombok 使用

@Data
注释生成 getter 和 setter。

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

可以从 GitHub 这里克隆完整代码。

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

API 响应:

{
    "responseData": [
        {
            "flightId": 1,
            "originAirport": "BOM",
            "destinationAirport": "DEL"
        }
    ],
    "statusCode": "OK",
    "message": "Flights Found",
    "messageType": "success"
}
© www.soinside.com 2019 - 2024. All rights reserved.