【问题标题】:REST API: Using a GET Request to match an ID within a File and display the object corresponding to IDREST API:使用 GET 请求匹配文件中的 ID 并显示与 ID 对应的对象
【发布时间】:2018-03-11 20:43:30
【问题描述】:

我确实需要关于我正在从事的 RESTful API 项目的帮助。我进行了广泛的搜索,现在正处于十字路口。这对我来说很新,我不太了解所有内容,所以如果这是一个基本问题,我深表歉意。

我需要做什么:

我需要创建一个“getVehicle”方法来执行以下操作:

  • getVehicle() 将获取给定的 id,并找到具有 匹配标识。
  • 它将逐行迭代本地文件,检查是否 id 匹配,如果匹配则返回车辆对象。

这是标题:

 @RequestMapping(value = "/getVehicle/{id}", method = RequestMethod.GET)
 public Vehicle getVehicle(@PathVariable("id") int id) throws IOException {

我确实知道如何使用纯 Java 来做到这一点,但是这种 RESTful 的东西让我陷入了循环。

我的理解:

据我所知,当我发出 GET 请求(为此我使用 Advance REST 客户端)时,我为 {id} 输入的 ID 将与我的文本文件(称为 inventory.txt )。

我使用 POST 方法在我的文本文件中填充了 6 个条目,所有这些条目都是关于各种“车辆”的信息。因此,如果我向 url http://localhost808/getVehicle/2 发出 GET 请求,我应该得到关于 id 为 2 的“车辆”的信息。

我的问题:

但是,就我目前拥有的而言,我的文件没有被迭代,所以我得到的唯一“车辆”是 id 0。

我目前所拥有的:

    @RequestMapping(value = "/getVehicle/{id}", method = RequestMethod.GET)
    public Vehicle getVehicle(@PathVariable("id") int id) throws IOException 
    {
        //ObjectMapper provides functionality for reading and writing JSON
        ObjectMapper mapper = new ObjectMapper();

        String inventory = FileUtils.readFileToString(new 
            File("./inventory.txt"),
                CharEncoding.UTF_8);

        //Deserialize JSON to vehicle object
        Vehicle vehicle = mapper.readValue(inventory, Vehicle.class);

        if (vehicle.getId() == id) {
                return vehicle;
        }

        return null;
    }

我的要求:

我不知道如何遍历我的文本文件以查找正在请求的 id。

弄清楚这一点将彻底消除我的困惑并帮助我完成我的项目

任何提示、技巧、资源以及任何能让我走上解决这个问题的道路的东西都将不胜感激。再说一次,这对我很好,我正在努力保持理智。

如果我需要澄清任何事情,请告诉我!

我要感谢所有花时间在这里帮助我的人:D!

【问题讨论】:

  • 我没有看到任何类型的迭代循环。我假设vehicle 返回一个对象,您需要使用for (v: vehicle) { if statement} 之类的东西对其进行迭代。
  • 我试过了,我得到一个错误,上面写着:“foreach 不适用于类型'com.example.Vehicle'”。我假设我不能将 foreach 与我的对象类型一起使用??
  • 你给它v 将是什么类型的变量?抱歉,我忘了添加这个for (Vehicle v: vehicle) { System.out.println(v); }。看看这是否有效。
  • 是的,我做到了。 “车辆”带有下划线,我之前提到的错误。我的包路径与此有关吗??

标签: java rest restful-url


【解决方案1】:

您不能遍历inventory,因为它是一个字符串。您可以遍历 vehicle,因为您正在将 inventory 字符串转换为 Vehicle 对象。

好的,我刚刚发现了一个错误。 Vehicle vehicle 这是一个单一的对象。你需要一个数组。

@RequestMapping(value = "/getVehicle/{id}", method = RequestMethod.GET,
                 produces = MediaType.APPLICATION_JSON_VALUE)
public Vehicle getVehicle(@PathVariable("id") int id) throws IOException {
    //ObjectMapper provides functionality for reading and writing JSON
    ObjectMapper mapper = new ObjectMapper();

    String inventory = FileUtils.readFileToString(new 
        File("./inventory.txt"),
            CharEncoding.UTF_8);

    //Deserialize JSON to vehicle object
    List<Vehicle> vehicle = (Vehicle)mapper.readValue(inventory, Vehicle.class);
    // Or
    List<Object> vehicle = mapper.readValue(inventory, Vehicle.class);
    // Recommend the first option, its better to use types, things less likely to go wrong.

    for (Vehicle v: vehicle){
      if (vehicle.getId() == id) {
        return vehicle;
      }
    }

    return null;
}

【讨论】:

  • 这就是为什么无论你通过什么 ID,你总是得到对象 0。它映射了第一个并丢弃了其余的。
  • 我试过了,现在我得到了 mapper.readValue(inventory, Vehicle.class) 的新错误。我强烈同意我需要一个迭代循环,并且我知道它必须处理“车辆”,但我对它迭代它所需的结构感到迷茫。我在想我需要用我的车辆条目填充一个列表,但是,我又该如何去做呢?
  • 错误:“不兼容的类型。必需的 List 但 'readValue' 被推断为 T:不存在类型变量的实例,因此 Vehicle 符合 List 推断变量 T 具有不兼容的边界:等式约束:车辆上限:Object, List "
  • 好的,所以它抱怨您正在尝试将没有类型 Object 的值映射到类型为 Vehicle 的变量。您可以创建 List&lt;Object&gt; 或将 Object 转换为 Vehicle 类型。我会更新答案。另外,你有我可以看看的 Git 存储库吗?动手解决问题要容易得多。
  • 我解决了。一位朋友建议使用 LineInterator 而不是我使用 String 库存所做的。这种方式更好,也更容易理解。非常感谢您的帮助!!!
【解决方案2】:

这个问题已经用 LineIterator 解决了!

我的代码在这里:

@RequestMapping(value = "/getVehicle/{id}", method = RequestMethod.GET)
public Vehicle getVehicle(@PathVariable("id") int id) throws IOException {

    //ObjectMapper provides functionality for reading and writing JSON
    ObjectMapper mapper = new ObjectMapper();

    //Makes inventory.txt an iterable object
    LineIterator inventory = FileUtils.lineIterator(new File("./inventory.txt"),
            CharEncoding.UTF_8);

    //Container for when vehicle is found
    String vehicle = "";

    //iterates through inventory.txt until it reaches the end or finds the vehicle via id
    while(inventory.hasNext()) { 
        //Stores each vehicle
        String tempVehicle = inventory.next(); 
        //Creates a vehicle object for JSON
        Vehicle v = mapper.readValue(tempVehicle, Vehicle.class); 

        //checks if id of JSON vehicle object matches the id passed through
        if(v.getId() == id) { 
            vehicle = tempVehicle;
        }
    }
    //Returns the found vehicle as a new Objectmapper (still a bit confused about this one)
    return new ObjectMapper().readValue(vehicle, Vehicle.class);
}

【讨论】:

    猜你喜欢
    • 2021-09-27
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 2018-05-07
    • 2018-04-26
    相关资源
    最近更新 更多