[英]How to access list inside another function in the same class Java
I made a post and get mapping inside my java controller layer.我发了一个帖子并在我的 java controller 层中获取映射。 The post mapping gathers data such as the starting date, end date, and number of Guests from the front end.后映射从前端收集数据,例如开始日期、结束日期和客人数量。
Inside the service layer, the first function filters the available apartments.在服务层内部,第一个 function 过滤可用的公寓。 However, I want to make a get mapping which can access the list of available apartments so this can be displayed in the front end.但是,我想制作一个可以访问可用公寓列表的获取映射,以便可以在前端显示。 I tried this in the service layer with the second function.我在服务层用第二个 function 尝试了这个。 Unfortunately, this function only shows the entire apartment list.不幸的是,这个 function 只显示了整个公寓列表。 Does anybody know how I can get a function that can access the list from the first function so that I can display the filtered available apartments?有谁知道我如何获得可以从第一个 function 访问列表的 function 以便我可以显示过滤后的可用公寓?
Apartment Controller Layer公寓Controller层
@PostMapping(value = "api/availableapartments")
public List<Apartment> getAvailableApartments(@RequestBody String request)throws JSONException, ParseException {
JSONObject jsonObject = new JSONObject(request);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = format.parse(String.valueOf(jsonObject.getString("startDate")));
Date endDate = format.parse(String.valueOf(jsonObject.getString("endDate")));
Integer numberOfBeds = Integer.parseInt(String.valueOf(jsonObject.getString("numberOfBeds")));
return apartmentService.getAvailableApartments(numberOfBeds, startDate, endDate);
}
@GetMapping(value = "api/availableapartments")
public List<Apartment> getAvailableApartments(){
return apartmentService.getAllAvailableApartments();
}
Apartment service layer公寓服务层
public class ApartmentService {
public List<Apartment> getAvailableApartments(Integer requestedBeds, Date startDate, Date endDate){
List<Apartment> allApartments = apartmentRepository.findAll();
List<Apartment> availableApartments = new ArrayList<>();
//going through all apartments
for(Apartment apartment : allApartments){
if(apartment.getAvailableBeds() >= requestedBeds){
if(reservationService.checkAvailability(apartment, startDate, endDate)){
availableApartments.add(apartment);
}
}
}
return availableApartments;
}
public List<Apartment> getAllAvailableApartments(){
List<Apartment> allApartments = apartmentRepository.findAll();
List<Apartment> allAvailableApartments = new ArrayList<>();
for(Apartment apartments : allApartments){
System.out.println(apartments.getApartmentId());
allAvailableApartments.add(apartments);
}
return allAvailableApartments;
}
}
```
Not sure I understood the question exactly, but in this scenario it's better not to use a POST request just to POST an object containing some search parameters.不确定我是否完全理解了这个问题,但在这种情况下,最好不要使用 POST 请求来发布包含一些搜索参数的 object。
Instead of the POST you should use a GET with parameters for the filtering.您应该使用带有参数的 GET 而不是 POST 进行过滤。 That way you do the filtering in the GET request and don't need to "access the POST" request.这样您就可以在 GET 请求中进行过滤,而无需“访问 POST”请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.