当前位置: 首页 > 知识库问答 >
问题:

Spring座控制器返回特定字段

韩智明
2023-03-14

我一直在思考使用Spring MVC设计JSON API的最佳方法。众所周知,IO很昂贵,因此我不想让客户端进行多次API调用以获得他们需要的东西。然而,与此同时,我不一定想归还厨房水槽。

例如,我正在开发一个类似于IMDB的游戏API,但用于视频游戏。

如果我返回与游戏相关的所有内容,它将看起来像这样。

/api/game/1

{
    "id": 1,
    "title": "Call of Duty Advanced Warfare",
    "release_date": "2014-11-24",
    "publishers": [
        {
            "id": 1,
            "name": "Activision"
        }
    ],
    "developers": [
        {
            "id": 1,
            "name": "Sledge Hammer"
        }
    ],
    "platforms": [
        {
            "id": 1,
            "name": "Xbox One",
            "manufactorer": "Microsoft",
            "release_date": "2013-11-11"
        },
        {
            "id": 2,
            "name": "Playstation 4",
            "manufactorer": "Sony",
            "release_date": "2013-11-18"
        },
        {
            "id": 3,
            "name": "Xbox 360",
            "manufactorer": "Microsoft",
            "release_date": "2005-11-12"
        }
    ],
    "esrbRating": {
        "id": 1,
        "code": "T",
        "name": "Teen",
        "description": "Content is generally suitable for ages 13 and up. May contain violence, suggestive themes, crude humor, minimal blood, simulated gambling and/or infrequent use of strong language."
    },
    "reviews": [
        {
            "id": 1,
            "user_id": 111,
            "rating": 4.5,
            "description": "This game is awesome"
        }
    ]
}

然而,他们可能不需要所有这些信息,但他们可能需要这些信息。从I/O和性能来看,调用所有内容似乎是个坏主意。

我考虑通过在请求中指定include参数来实现这一点。

现在,例如,如果您没有指定任何包含,那么您将返回以下内容。

{
    "id": 1,
    "title": "Call of Duty Advanced Warfare",
    "release_date": "2014-11-24"
}

然而,如果您希望获得您请求的所有信息,那么这些信息都会是这样的。

/api/game/1?include=publishers,developers,platforms,reviews,esrbRating

这样,客户机就可以指定他们需要多少信息。然而,我对使用Spring MVC实现这一点的最佳方法有些茫然。

我想控制器应该是这样的。

public @ResponseBody Game getGame(@PathVariable("id") long id, 
    @RequestParam(value = "include", required = false) String include)) {

        // check which include params are present

        // then someone do the filtering?
}

我不确定您将如何选择序列化Game对象。这可能吗?在Spring MVC中解决这个问题的最佳方法是什么?

仅供参考,我正在使用Spring Boot,其中包括用于序列化的Jackson。

共有3个答案

胡新
2023-03-14

这可以由SpringProjments完成。也适用于静态编程语言。看这里:https://www.baeldung.com/spring-data-jpa-projections

束涵涤
2023-03-14

意识到我的答案来得很晚:我建议看一下预测。

你要求的是预测是关于什么的。

既然你问的是Spring,我想试试这个:https://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-摘录

GraphQL提供了一种根据需求提供不同预测的非常动态的方法。我刚刚看到一篇非常有用的文章,介绍了如何将GraphQL与SpringBoot结合使用:https://www.graphql-java.com/tutorials/getting-started-with-spring-boot/

陆正德
2023-03-14

您可以将其序列化为Map,而不是返回Game对象

@ResponseBody
public Map<String, Object> getGame(@PathVariable("id") long id, String include) {

    Game game = service.loadGame(id);
    // check the `include` parameter and create a map containing only the required attributes
    Map<String, Object> gameMap = service.convertGameToMap(game, include);

    return gameMap;

}

例如,如果您有一个映射

gameMap.put("id", game.getId());
gameMap.put("title", game.getTitle());
gameMap.put("publishers", game.getPublishers());

它将被序列化如下:

{
  "id": 1,
  "title": "Call of Duty Advanced Warfare",
  "publishers": [
    {
        "id": 1,
        "name": "Activision"
    }
  ]
}

 类似资料:
  • 如果可以从Spring返回,我很好奇 这样做可以吗?我试过了,Spring返回的不是流的值。 我应该继续返回

  • 我很难理解mono 的用法。请问这个Mono 能给Mono带来什么样的好处? 谢谢。

  • 我正在编写一个Spring Boot应用程序。我编写了一个简单的控制器,每当endpoint被命中时就会被调用,但它仍然返回状态404,而不是指定的返回值。 HelloController 现在,每当我调用时,我都会看到控制台日志,但是永远不会返回。邮递员输出: 应用JAVA

  • 我得到这个错误消息: 无法写入JSON: 未找到用于java.io.FileDescriptor类的序列化程序,也未发现用于创建BeanSerializer的属性 (为了避免异常,禁用SerializationFeature.fail_on_empty_beans)) (通过引用链: org.springframework.core.io.filesystemResource[\“outputSt

  • 编辑:下面是LatherupApplication应用程序类: 下面是我家控制器: 下面是pom.xml文件: 下面是home.jsp: 应用程序.属性:

  • 我需要做一个自定义行为,当用户按下后退按钮时,用户将通过编程方式前往某个目的地。实际上,我已经在Android导航组件中阅读了这个返回处理按钮 但是我不知道如何使用自定义后退按钮code.it对我来说似乎很奇怪。 我试过使用下面的代码