简体   繁体   English

如何将 JSON 从静态文件传递到休息控制器 SpringBoot App

[英]How to pass JSON from static file to rest controller SpringBoot App

I have a stable SpringBoot project that runs.我有一个稳定的 SpringBoot 项目可以运行。 I want to add a end point that reads a json file from classpath and passes it through to the response without having to create any Model objects (pass thru).我想添加一个端点,该端点从类路径读取 json 文件并将其传递给响应,而无需创建任何模型对象(传递)。

I have no issues reading the json file into JsonNode or ObjectNode, I'm struggling with where to go next to set the data in my response object.我在将 json 文件读入 JsonNode 或 ObjectNode 时没有任何问题,我正在为下一步在响应对象中设置数据而苦恼。

Added this caveat later, I do need to update the json from a database.稍后添加此警告,我确实需要从数据库更新 json。

Ok, paired up with a colleague at work and we tried two things, return a string (escapes strings in REST output - returns a big String.) not good.好的,与工作中的同事配对,我们尝试了两件事,返回一个字符串(在 REST 输出中转义字符串 - 返回一个大字符串。)不好。 What worked is setting the response object to a and calling mapper.readValue(jsonFeed, Map.class), that returned the JSON in proper object notation.有效的是将响应对象设置为 a 并调用 mapper.readValue(jsonFeed, Map.class),以正确的对象表示法返回 JSON。

    @Value("${metadata.json.file}") //defined in application.context
    private Resource metaJsonFileName;

    public String getJsonFromFile(List<UnitUiItem> uiitems){
        JsonNode root;
        ObjectMapper mapper = new ObjectMapper();
        InputStream stream = metaJsonFileName.getInputStream();

        root = mapper.readTree(stream);
        JsonNode dataNode = root.get("data");
        JsonNode optionDataNode = dataNode.get("storelocation");

        ((ObjectNode)optionDataNode).putArray("units");
       for(UnitUiItem item : uiitems){
        JsonNode unitNode = ((ObjectNode)optionDataNode).withArray("units").addObject();
        ((ObjectNode)unitNode).put("code",item.getCode());
        ((ObjectNode)unitNode).put("displayName",item.getDisplayName());
    }

    LOGGER.info("buildMetaJson exit");
    return root.toString();
    }

    //calling method
    String jsonFeed = getJsonFromFile();
    ObjectMapper mapper = new ObjectMapper();
    response.setData(mapper.readValue(jsonFeed, Map.class));

I have some code cleanup to do.. any cleaner ways of doing this?我有一些代码清理工作要做.. 任何更简洁的方法来做到这一点?

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

相关问题 How to pass JSON Object and return Object from Spring rest controller - How to pass JSON Object and return Object from Spring rest controller 如何使用 Springboot 在 REST 请求 URL 中将 json 对象作为参数传递 - How to pass a json object as a parameter in a REST request URL with Springboot 如何在具有来自 Controller 的自定义路径的 springboot 中提供 static 资源? - How to serve static resource in springboot having custom path from Controller? SpringBoot:如何将数据从一个控制器传递到另一个控制器? - SpringBoot: How to pass data from one controller to another? 使用 Mockito 测试我的 SpringBoot 应用程序时如何从属性文件传递变量? - How to pass a variable from properties file when testing my SpringBoot app with Mockito? Springboot,使用Rest Controller从对象访问字段 - Springboot, accessing field from object with Rest Controller 将参数从 jsp 传递给 springboot 控制器 - pass parameter from jsp to springboot controller 如何将@value注解的值传递给SpringBoot中的controller - How to pass the value of an @value annotation to the controller in SpringBoot 如何从控制器传递json字符串以查看 - how to pass the json string from controller to view 如何将JSON数组从AngularJS控制器传递给Spring Controller? - How to pass JSON array from AngularJS Controller to the Spring Controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM