json - Spring webflux Netty : How to expose proto

用例:

开发人员/我只想实现 Protobuf 实现(二进制协议(protocol))。但是,我需要一种添加配置的方法,因此,同样的实现也作为 rest/json api 公开——没有代码重复

我暴露了原型(prototype)端点。我还希望消费者发布 json 等效于那些原型(prototype)对象,并返回/接收 json 等效于具有类型信息(Pojo?)的结果。类型信息也有助于 OpenAPI/Swagger 文档!

在没有代码重复的情况下实现该目标的最优雅/最简单的方法是什么?

任何实现该目标的示例 github 代码都会有所帮助。

注意:这是针对 webflux 和 netty - 不是 tomcat。

ProtobufJsonFormatHttpMessageConverter - 适用于 tomcat,不适用于 netty。一个有效的示例代码会很棒。

最佳答案

我一直在胡思乱想,最后得到了这个。没有别的对我有用。 使用 protov3 并像这样设置 protobuf

syntax = "proto3";
option java_package = "com.company";
option java_multiple_files = true;

message CreateThingRequest {
...

message CreateThingResponse {
....

我可以通过在我的 application.properties 中设置 app.protoPath 来扫描 protobuf 文件

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.google.common.reflect.ClassPath;
import com.google.protobuf.Message;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.reactive.config.WebFluxConfigurer;


@Configuration
public class WebConfig implements WebFluxConfigurer {

  @Value("${app.protoPath:com.}")
  private String protoPath;

  @Override
  public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
    configurer.defaultCodecs().jackson2JsonEncoder(
        new Jackson2JsonEncoder(Jackson2ObjectMapperBuilder.json().serializerByType(
            Message.class, new JsonSerializer<Message>() {
              @Override
              public void serialize(Message value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
                String str = JsonFormat.printer().omittingInsignificantWhitespace().print(value);
                gen.writeRawValue(str);
              }
            }
        ).build())
    );

    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    Map<Class<?>, JsonDeserializer<?>> deserializers = new HashMap<>();
    try {
      for (final ClassPath.ClassInfo info : ClassPath.from(loader).getTopLevelClasses()) {
        if (info.getName().startsWith(protoPath)) {
          final Class<?> clazz = info.load();

          if (!Message.class.isAssignableFrom(clazz)) {
            continue;
          }
          @SuppressWarnings("unchecked") final Class<Message> proto = (Class<Message>) clazz;

          final JsonDeserializer<Message> deserializer = new CustomJsonDeserializer() {
            @Override
            public Class<Message> getDeserializeClass() {
              return proto;
            }
          };
          deserializers.put(proto, deserializer);
        }
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    configurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(Jackson2ObjectMapperBuilder.json().deserializersByType(deserializers).build()));
  }

  private abstract static class CustomJsonDeserializer extends JsonDeserializer<Message> {
    abstract Class<? extends Message> getDeserializeClass();

    @Override
    public Message deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
      Message.Builder builder = null;
      try {
        builder = (Message.Builder) getDeserializeClass()
            .getDeclaredMethod("newBuilder")
            .invoke(null);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      JsonFormat.parser().merge(jp.getCodec().readTree(jp).toString(), builder);
      return builder.build();
    }
  }
}

然后我只在返回中使用对象类型;

@PostMapping(
    path = "/things",
    consumes = {MediaType.APPLICATION_JSON_VALUE, "application/x-protobuf"},
    produces = {MediaType.APPLICATION_JSON_VALUE, "application/x-protobuf"})
Mono<CreateThingResponse> createThing(@RequestBody CreateThingRequest request);

与 https://github.com/innogames/springfox-protobuf您可以大摇大摆地显示响应,但仍然没有向我显示请求。

请原谅我有点生疏的乱七八糟的 Java。

关于json - Spring webflux Netty : How to expose proto as json endpoints without duplication of code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60289736/

相关文章:

javascript - 如何使用 JavaScript 观察 an 元素的 scrollWidth

python - pytest:带参数的自定义标记

google-chrome - 如何在 .Net framework 4.0.30319 中设置 H

javascript - 错误 : Objects are not valid as a React

javascript - 如何在 Vuejs 上的 CKEditor 5 上激活和/或安装插件

facebook - 在 Instagram Basic Display API 中获取 "app

linux - git-lfs/debian 没有发布文件

google-cloud-platform - 如何部署使用google cloud build和S

python-3.8 - Python 3.8 使用 PIP 下载包/模块错误

google-chrome - 如何告诉谷歌浏览器尊重 Xfce 上的系统默认文件浏览器 (Thun