SpringBoot / JHipster rest api配置,transtyping问题

时间:2017-11-14 18:03:53

标签: spring-boot jhipster

当我尝试在JsonFormat中获取我的一个对象时,我的Jackster版本4.2.2生成的SpringBoot rest api出了问题。

    Request URL
    http://localhost:3000/api/models
    Response Body
    [
      {
        "id": 1,
        "name": "lenet",
        "lossWeights": null,
        "loss": "categorical_crossentropy",
        "sampleWeightMode": null,
        "isInput": false,
        "isOutput": false,
        "optimizer": {
          "id": 1,
          "optimizerType": {
        "id": 5,
        "name": "SGD"
          }
        },
        "coordinates": {
          "id": 1,
          "xpos": 0,
          "ypos": 0
        },
        "containedModels": [],
        "succeededByModels": [],
        "succeededByLayers": [],
        "containedLayers": [
          {
        "id": 1,
        "name": "input",
        "isInput": true,
        "isOutput": false,
        "isIntermediate": false,
        "coordinates": {
          "id": 2,
          "xpos": 78.376953125,
          "ypos": 108.01763916015625
        },
        "succeededByLayers": null,
        "succeededByModels": null,
        "layerType": {
          "id": 43,
          "name": "InputLayer"
        }
          },
          {
        "id": 2,
        "name": "conv2d_1",
        "isInput": false,
        "isOutput": false,
        "isIntermediate": false,
        "coordinates": {
          "id": 3,
          "xpos": 341.5582275390625,
          "ypos": 102.59124755859375
        },
        "succeededByLayers": null,
        "succeededByModels": null,
        "layerType": {
          "id": 63,
          "name": "Conv2D"
        }
          },
          {
        "id": 3,
        "name": "activation_1",
        "isInput": false,
        "isOutput": false,
        "isIntermediate": false,
        "coordinates": {
          "id": 4,
          "xpos": 623.73193359375,
          "ypos": 99.8780517578125
        },
        "succeededByLayers": null,
        "succeededByModels": null,
        "layerType": {
          "id": 15,
          "name": "Activation"
        }
          },
          {
        "id": 4,
        "name": "max_pooling2d_1",
        "isInput": false,
        "isOutput": false,
        "isIntermediate": false,
        "coordinates": {
          "id": 5,
          "xpos": 941.17724609375,
          "ypos": 102.59124755859375
        },
        "succeededByLayers": null,
        "succeededByModels": null,
        "layerType": {
          "id": 70,
          "name": "MaxPooling2D"
        }
          },
          {
        "id": 5,
        "name": "conv2d_2",
        "isInput": false,
        "isOutput": false,
        "isIntermediate": false,
        "coordinates": {
          "id": 6,
          "xpos": 1253.1962890625,
          "ypos": 99.8780517578125
        },
        "succeededByLayers": null,
        "succeededByModels": null,
        "layerType": {
          "id": 63,
          "name": "Conv2D"
        }
          },
          {
        "id": 6,
        "name": "max_pooling2d_2",
        "isInput": false,
        "isOutput": false,
        "isIntermediate": false,
        "coordinates": {
          "id": 7,
          "xpos": 1540.2081298828125,
          "ypos": 99.8780517578125
        },
        "succeededByLayers": null,
        "succeededByModels": null,
        "layerType": {
          "id": 70,
          "name": "MaxPooling2D"
        }
          },
          {
        "id": 7,
        "name": "activation_2",
        "isInput": false,
        "isOutput": false,
        "isIntermediate": false,
        "coordinates": {
          "id": 8,
          "xpos": 1861.44921875,
          "ypos": 99.8780517578125
        },
        "succeededByLayers": null,
        "succeededByModels": null,
        "layerType": {
          "id": 15,
          "name": "Activation"
        }
          },
          {
        "id": 8,
        "name": "flatten_1",
        "isInput": false,
        "isOutput": false,
        "isIntermediate": false,
        "coordinates": {
          "id": 9,
          "xpos": 2151.32373046875,
          "ypos": 99.8780517578125
        },
        "succeededByLayers": null,
        "succeededByModels": null,
        "layerType": {
          "id": 54,
          "name": "Flatten"
        }
          },
          {
        "id": 9,
        "name": "dense_1",
        "isInput": false,
        "isOutput": false,
        "isIntermediate": false,
        "coordinates": {
          "id": 10,
          "xpos": 2505.89208984375,
          "ypos": 98.57888793945312
        },
        "succeededByLayers": null,
        "succeededByModels": null,
        "layerType": {
          "id": 6,
          "name": "Dense"
        }
          },
          {
        "id": 10,
        "name": "dropout_1",
        "isInput": false,
        "isOutput": false,
        "isIntermediate": false,
        "coordinates": {
          "id": 11,
          "xpos": 2828.763671875,
          "ypos": 99.8780517578125
        },
        "succeededByLayers": null,
        "succeededByModels": null,
        "layerType": {
          "id": 44,
          "name": "Dropout"
        }
          },
          {
        "id": 11,
        "name": "dense_2",
        "isInput": false,
        "isOutput": false,
        "isIntermediate": false,
        "coordinates": {
          "id": 12,
          "xpos": 3121.79052734375,
          "ypos": 99.8780517578125
        },
        "succeededByLayers": null,
        "succeededByModels": null,
        "layerType": {
          "id": 6,
          "name": "Dense"
        }
          },
          {
        "id": 12,
        "name": "dropout_2",
        "isInput": false,
        "isOutput": false,
        "isIntermediate": false,
        "coordinates": {
          "id": 13,
          "xpos": 3452.8017578125,
          "ypos": 99.8780517578125
        },
        "succeededByLayers": null,
        "succeededByModels": null,
        "layerType": {
          "id": 44,
          "name": "Dropout"
        }
          },
          {
        "id": 13,
        "name": "dense_3",
        "isInput": false,
        "isOutput": false,
        "isIntermediate": false,
        "coordinates": {
          "id": 14,
          "xpos": 3784.675048828125,
          "ypos": 99.8780517578125
        },
        "succeededByLayers": null,
        "succeededByModels": null,
        "layerType": {
          "id": 6,
          "name": "Dense"
        }
          }
        ],
        "modelConfiguration": {
          "id": 1,
          "name": "Modèle LeNet5",
          "creationDate": "2017-05-11T00:00:00+02:00",
          "saveURI": "./tests/scripts/sequential/models/model.json",
          "project": {
        "id": 1,
        "name": "LeNet5",
        "creationDate": "2017-10-22T00:00:00+02:00",
        "updateDate": "2017-10-23T00:00:00+02:00"
          }
        }
      }
    ]

就像你上面看到的那样,这代表了我的模特。你可以通过字段containdLayers访问里面的图层列表,每个图层都有一个字段suceededBylayers,在这种情况下不应该为null(对于id 2)

        @GetMapping("/models/{id}")
        @Timed
        public ResponseEntity<Model> getModel(@PathVariable Long id) {
        log.debug("REST request to get Model : {}", id);
        Model model = modelRepository.findOneWithEagerRelationships(id);
        System.out.println("======================================================");
        // Layer layer = (Layer)model.getContainedLayers().toArray()[0];
        // System.out.println(((Layer)layer.getSucceededByLayers().toArray()[0]).getSucceededByLayers());
        System.out.println("=====================================================");
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(model));
        }

这是java spring代码中可以获取此类Json文件的部分。当我取消注释i transtype的行时,我得到了这种Json文件

      {
                  "id": 1,
                  "name": "lenet",
                  "lossWeights": null,
                  "loss": "categorical_crossentropy",
                  "sampleWeightMode": null,
                  "isInput": false,
                  "isOutput": false,
                  "optimizer": {
                    "id": 1,
                    "optimizerType": {
                      "id": 5,
                      "name": "SGD"
                    }
                  },
                  "coordinates": {
                    "id": 1,
                    "xpos": 0,
                    "ypos": 0
                  },
                  "containedModels": [],
                  "succeededByModels": [],
                  "succeededByLayers": [],
                  "containedLayers": [
                    {
                      "id": 1,
                      "name": "input",
                      "isInput": true,
                      "isOutput": false,
                      "isIntermediate": false,
                      "coordinates": {
                    "id": 2,
                    "xpos": 78.376953125,
                    "ypos": 108.01763916015625
                      },
                      "succeededByLayers": [
                    {
                      "id": 2,
                      "name": "conv2d_1",
                      "isInput": false,
                      "isOutput": false,
                      "isIntermediate": false,
                      "coordinates": {
                        "id": 3,
                        "xpos": 341.5582275390625,
                        "ypos": 102.59124755859375
                      },
                      "succeededByLayers": [
                        {
                          "id": 3,
                          "name": "activation_1",
                          "isInput": false,
                          "isOutput": false,
                          "isIntermediate": false,
                          "coordinates": {
                        "id": 4,
                        "xpos": 623.73193359375,
                        "ypos": 99.8780517578125
                          },
                          "succeededByLayers": null,
                          "succeededByModels": null,
                          "layerType": {
                        "id": 15,
                        "name": "Activation"
                          }
                        }
                      ],
                      "succeededByModels": null,
                      "layerType": {
                        "id": 63,
                        "name": "Conv2D"
                      }
                    }
                      ],
                      "succeededByModels": null,
                      "layerType": {
                    "id": 43,
                    "name": "InputLayer"
                      }
                    },
                    {
                      "id": 2,
                      "name": "conv2d_1",
                      "isInput": false,
                      "isOutput": false,
                      "isIntermediate": false,
                      "coordinates": {
                    "id": 3,
                    "xpos": 341.5582275390625,
                    "ypos": 102.59124755859375
                      },
                      "succeededByLayers": null,
                      "succeededByModels": null,
                      "layerType": {
                    "id": 63,
                    "name": "Conv2D"
                      }
                    },
                    {
                      "id": 3,
                      "name": "activation_1",
                      "isInput": false,
                      "isOutput": false,
                      "isIntermediate": false,
                      "coordinates": {
                    "id": 4,
                    "xpos": 623.73193359375,
                    "ypos": 99.8780517578125
                      },
                      "succeededByLayers": null,
                      "succeededByModels": null,
                      "layerType": {
                    "id": 15,
                    "name": "Activation"
                      }
                    },
                    {
                      "id": 4,
                      "name": "max_pooling2d_1",
                      "isInput": false,
                      "isOutput": false,
                      "isIntermediate": false,
                      "coordinates": {
                    "id": 5,
                    "xpos": 941.17724609375,
                    "ypos": 102.59124755859375
                      },
                      "succeededByLayers": null,
                      "succeededByModels": null,
                      "layerType": {
                    "id": 70,
                    "name": "MaxPooling2D"
                      }
                    },
                    {
                      "id": 5,
                      "name": "conv2d_2",
                      "isInput": false,
                      "isOutput": false,
                      "isIntermediate": false,
                      "coordinates": {
                    "id": 6,
                    "xpos": 1253.1962890625,
                    "ypos": 99.8780517578125
                      },
                      "succeededByLayers": null,
                      "succeededByModels": null,
                      "layerType": {
                    "id": 63,
                    "name": "Conv2D"
                      }
                    },
                    {
                      "id": 6,
                      "name": "max_pooling2d_2",
                      "isInput": false,
                      "isOutput": false,
                      "isIntermediate": false,
                      "coordinates": {
                    "id": 7,
                    "xpos": 1540.2081298828125,
                    "ypos": 99.8780517578125
                      },
                      "succeededByLayers": null,
                      "succeededByModels": null,
                      "layerType": {
                    "id": 70,
                    "name": "MaxPooling2D"
                      }
                    },
                    {
                      "id": 7,
                      "name": "activation_2",
                      "isInput": false,
                      "isOutput": false,
                      "isIntermediate": false,
                      "coordinates": {
                    "id": 8,
                    "xpos": 1861.44921875,
                    "ypos": 99.8780517578125
                      },
                      "succeededByLayers": null,
                      "succeededByModels": null,
                      "layerType": {
                    "id": 15,
                    "name": "Activation"
                      }
                    },
                    {
                      "id": 8,
                      "name": "flatten_1",
                      "isInput": false,
                      "isOutput": false,
                      "isIntermediate": false,
                      "coordinates": {
                    "id": 9,
                    "xpos": 2151.32373046875,
                    "ypos": 99.8780517578125
                      },
                      "succeededByLayers": null,
                      "succeededByModels": null,
                      "layerType": {
                    "id": 54,
                    "name": "Flatten"
                      }
                    },
                    {
                      "id": 9,
                      "name": "dense_1",
                      "isInput": false,
                      "isOutput": false,
                      "isIntermediate": false,
                      "coordinates": {
                    "id": 10,
                    "xpos": 2505.89208984375,
                    "ypos": 98.57888793945312
                      },
                      "succeededByLayers": null,
                      "succeededByModels": null,
                      "layerType": {
                    "id": 6,
                    "name": "Dense"
                      }
                    },
                    {
                      "id": 10,
                      "name": "dropout_1",
                      "isInput": false,
                      "isOutput": false,
                      "isIntermediate": false,
                      "coordinates": {
                    "id": 11,
                    "xpos": 2828.763671875,
                    "ypos": 99.8780517578125
                      },
                      "succeededByLayers": null,
                      "succeededByModels": null,
                      "layerType": {
                    "id": 44,
                    "name": "Dropout"
                      }
                    },
                    {
                      "id": 11,
                      "name": "dense_2",
                      "isInput": false,
                      "isOutput": false,
                      "isIntermediate": false,
                      "coordinates": {
                    "id": 12,
                    "xpos": 3121.79052734375,
                    "ypos": 99.8780517578125
                      },
                      "succeededByLayers": null,
                      "succeededByModels": null,
                      "layerType": {
                    "id": 6,
                    "name": "Dense"
                      }
                    },
                    {
                      "id": 12,
                      "name": "dropout_2",
                      "isInput": false,
                      "isOutput": false,
                      "isIntermediate": false,
                      "coordinates": {
                    "id": 13,
                    "xpos": 3452.8017578125,
                    "ypos": 99.8780517578125
                      },
                      "succeededByLayers": null,
                      "succeededByModels": null,
                      "layerType": {
                    "id": 44,
                    "name": "Dropout"
                      }
                    },
                    {
                      "id": 13,
                      "name": "dense_3",
                      "isInput": false,
                      "isOutput": false,
                      "isIntermediate": false,
                      "coordinates": {
                    "id": 14,
                    "xpos": 3784.675048828125,
                    "ypos": 99.8780517578125
                      },
                      "succeededByLayers": null,
                      "succeededByModels": null,
                      "layerType": {
                    "id": 6,
                    "name": "Dense"
                      }
                    }
                  ],
                  "modelConfiguration": {
                    "id": 1,
                    "name": "Modèle LeNet5",
                    "creationDate": "2017-05-11T00:00:00+02:00",
                    "saveURI": "./tests/scripts/sequential/models/model.json",
                    "project": {
                      "id": 1,
                      "name": "LeNet5",
                      "creationDate": "2017-10-22T00:00:00+02:00",
                      "updateDate": "2017-10-23T00:00:00+02:00"
                    }
                  }
                }

这里的字段不是null。我想如何配置api所以我没有得到一个空字段。我也很想获得只有一个层ID而不是实际的下一层的数组。 我不知道我的第一篇文章是否足够清楚。不要再问我更多信息。

1 个答案:

答案 0 :(得分:0)

您在JPA中遇到了延迟加载的关系。默认情况下,JHipster中的所有关系都是延迟加载的,这意味着除非您访问它们,否则不会获取相关实体。这就是为什么值为null,除非您在System.out.println调用中访问它们,这使得Hibernate获取相关对象。

要解决此问题,您应修改存储库并使用@EntityGraph,查看Spring Data JPA文档:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.entity-graph