简体   繁体   English

如何在同一个 class Java 中访问另一个 function 中的列表

[英]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”请求。

Have a look here 看看这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个类访问另一个类函数(java) - How to access another class function from another class (java) 如何从Java中的另一个类访问一个类的函数的变量 - How to access the variables of a function of a class from another class in Java 如何访问位于Java中另一个类的另一个方法内的类内的方法 - How can I access a method that is inside a class that is inside another method of another class in Java 如何在同一 class 中的 java 中访问存储在另一种方法中的变量 - How to access a variable stored in another method in java in the same class 如何访问java中类构造函数内的数组列表? - How do I access an array list inside of a class constructor in java? 如何访问在中指定的自定义类对象的Java列表 <s:iterator> 我的javascript函数中的jsp文件标签? - How to access the java List of a custom class objects specified in <s:iterator> tag of jsp file inside my javascript function? 来自另一个类的Java链表访问 - java linked list access from another class java - 如何访问另一个文件中另一个类的方法,该文件在java中的同一个包中? - How to access a method of another class in another file which is in same package in java? 如何在Java中同一包中的不同文件上从一个类访问另一个类的变量 - How to access a variable from one class into another class on different files in same package in java 如何从 Java 应用程序中的另一个类访问在实用程序类中声明的公共最终静态列表? - How can I access to a public final static list declared into an utility class from another class in a Java application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM