配置Protobuf或Protobufhttpmessageconverter返回null或空字段

发布于 2025-01-24 08:39:07 字数 1352 浏览 4 评论 0 原文

protobuf 在Java生态系统中有所了解,我当前正在尝试配置端点以返回 all fields,尽管当前,端点仅返回 nonnon-null non-empty non-Zero 字段。

通常,我会做类似的事情,例如在 application.yaml 之类的类似中,我想知道是否有某种方法可以配置Protobufhttpmessageconverter,以给我带来类似的结果

spring:
  jackson:
    default-property-inclusion: ALWAYS

,这是我的 proteg> protabobufmessageConverterConferterConfig

@Configuration
public class ProtobufMessageConverterConfig {

  @Bean
  public ProtobufHttpMessageConverter protobufHttpMessageConverter() {
    return new ProtobufHttpMessageConverter();
  }
}

和我的控制器方法,该方法使用标准 grpc-java 插件返回我的原型生成的模型:

@GetMapping(value = "/assets", produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<GetAssetsResponse> getAssets() {

    // do something to populate GetAssetsResponse
    final var reply = GetAssetsResponse.newBuilder().build();

    return new ResponseEntity<>(reply, HttpStatus.OK);
  }

所以,我的问题是:

  1. 我可以配置ProtoBufMessageConverterConfig以确保所有字段(包括0或空字段)都会在我的响应中返回,或者
  2. 可以/应该/应该在原触发中添加属性以确保这些字段被填充吗?

谢谢!

Somewhat new to protobuf in the java ecosystem I am currently trying to configure an endpoint to return all fields, although currently, the endpoint only returns non-null, non-empty, non-zero fields.

Usually, I would do something like configure object mapper in application.yaml like so, and I am wondering if there is someway I can configure ProtobufHttpMessageConverter to yield me similar results

spring:
  jackson:
    default-property-inclusion: ALWAYS

for context, here's my ProtobufMessageConverterConfig

@Configuration
public class ProtobufMessageConverterConfig {

  @Bean
  public ProtobufHttpMessageConverter protobufHttpMessageConverter() {
    return new ProtobufHttpMessageConverter();
  }
}

and my Controller method that returns the model generated from my proto using the standard grpc-java plugin:

@GetMapping(value = "/assets", produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<GetAssetsResponse> getAssets() {

    // do something to populate GetAssetsResponse
    final var reply = GetAssetsResponse.newBuilder().build();

    return new ResponseEntity<>(reply, HttpStatus.OK);
  }

SO, my question is:

  1. Can I configure ProtobufMessageConverterConfig to ensure all fields (including 0 or empty fields) get returned in my response or
  2. Can/should I add properties to my proto to ensure these fields get populated?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文