如何使用请求方法读取嵌套的Json文件来创建端点?

时间:2019-05-29 09:43:10

标签: java json spring controller

我已经嵌套了Json文件,我想使用请求方法从Json文件读取特定对象。主要是为了制作我需要的端点。我什至试图阅读所有内容,但没有用。 因此,我需要与此相关的帮助,我的意思是如何读取整个Json文件,之后,我认为我可以处理像我需要的端点: / api / category / {categoryName} / books

我arleady尝试过这样的事情:

@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody
    Object getBeers() {
        ClassPathResource resource = new ClassPathResource("static/books.json");
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readValue(resource.getInputStream(), Object.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;      //also tried with path here
    }

我得到这样的结果: (java.io.FileNotFoundException:类路径资源[static / books.json]无法打开,因为它不存在)

我尝试了其他所有方式的写路径。不工作 所以我想我做错了

如果有人需要,这里是整个json: https://pastebin.com/yruFS5SM 下面是我的Json的一部分,此后重复使用类似的对象。

{
 "requestedUrl": "https://www.googleapis.com/books/v1/volumes?q=java&maxResults=40",
 "items": [
  {
   "kind": "books#volume",
   "id": "7tkN1CYzn2cC",
   "etag": "pfjjxSpetIM",
   "selfLink": "https://www.googleapis.com/books/v1/volumes/7tkN1CYzn2cC",
   "volumeInfo": {
    "title": "A Hypervista of the Java Landscape",
    "publisher": "InfoStrategist.com",
    "industryIdentifiers": [
     {
      "type": "ISBN_13",
      "identifier": "9781592432172"
     },
     {
      "type": "ISBN_10",
      "identifier": "1592432174"
     }
    ],
    "readingModes": {
     "text": true,
     "image": true
    },
    "printType": "BOOK",
    "maturityRating": "NOT_MATURE",
    "allowAnonLogging": false,
    "contentVersion": "1.0.1.0.preview.3",
    "imageLinks": {
     "smallThumbnail": "http://books.google.com/books/content?id=7tkN1CYzn2cC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
     "thumbnail": "http://books.google.com/books/content?id=7tkN1CYzn2cC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
    },
    "language": "en",
    "previewLink": "http://books.google.pl/books?id=7tkN1CYzn2cC&pg=PP1&dq=java&hl=&cd=1&source=gbs_api",
    "infoLink": "http://books.google.pl/books?id=7tkN1CYzn2cC&dq=java&hl=&source=gbs_api",
    "canonicalVolumeLink": "https://books.google.com/books/about/A_Hypervista_of_the_Java_Landscape.html?hl=&id=7tkN1CYzn2cC"
   },
   "saleInfo": {
    "country": "PL",
    "saleability": "NOT_FOR_SALE",
    "isEbook": false
   },
   "accessInfo": {
    "country": "PL",
    "viewability": "PARTIAL",
    "embeddable": true,
    "publicDomain": false,
    "textToSpeechPermission": "ALLOWED",
    "epub": {
     "isAvailable": true,
     "acsTokenLink": "http://books.google.pl/books/download/A_Hypervista_of_the_Java_Landscape-sample-epub.acsm?id=7tkN1CYzn2cC&format=epub&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api"
    },
    "pdf": {
     "isAvailable": true,
     "acsTokenLink": "http://books.google.pl/books/download/A_Hypervista_of_the_Java_Landscape-sample-pdf.acsm?id=7tkN1CYzn2cC&format=pdf&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api"
    },
    "webReaderLink": "http://play.google.com/books/reader?id=7tkN1CYzn2cC&hl=&printsec=frontcover&source=gbs_api",
    "accessViewStatus": "SAMPLE",
    "quoteSharingAllowed": false
   }
  }

0 个答案:

没有答案