title: Sending protobuf as JSON in spring-boot
body: <p>I´m using protobufs with this concrete definition.</p>
  
  <pre><code>message Hash {
      string category = 1;
      repeated KVPair content = 2;
  }
  
  message KVPair {
      string key = 1;
      string value = 2;
  }
  </code></pre>
  
  <p>I want to send this as JSON with my spring-boot application.
  I added this package to my gradle dependencies:</p>
  
  <pre><code>compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.6.1'
  </code></pre>
  
  <p>When i try to output Hash generated object with this code:</p>
  
  <pre><code>@RestController
  @RequestMapping("/api/crm/")
  public class KVController {
  
      private final KVService kvService;
  
      public KVController(KVService kvService) {
          this.kvService = kvService;
      }
  
      @GetMapping("kv/{category}")
      public Hash getHash(@PathVariable String category) {
          Hash hash = kvService.retrieve(category);
          return hash;
      }
  }
  </code></pre>
  
  <p>It throws this ultimate exception:</p>
  
  <blockquote>
    <p>Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Direct self-reference leading to cycle (through reference chain: com.blaazha.crm.proto.Hash["unknownFields"]->com.google.protobuf.UnknownFieldSet["defaultInstanceForType"])
        at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.9.6.jar:2.9.6]
        at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191) ~[jackson-databind-2.9.6.jar:2.9.6]
        at com.fasterxml.jackson.databind.ser.BeanPropertyWriter._handleSelfReference(BeanPropertyWriter.java:944) ~[jackson-databind-2.9.6.jar:2.9.6]
        at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:721) ~[jackson-databind-2.9.6.jar:2.9.6]
        at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) ~[jackson-databind-2.9.6.jar:2.9.6]
        at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.6.jar:2.9.6]
        at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) ~[jackson-databind-2.9.6.jar:2.9.6]
        at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) ~[jackson-databind-2.9.6.jar:2.9.6]
        at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.6.jar:2.9.6]
        at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480) ~[jackson-databind-2.9.6.jar:2.9.6]
        at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319) ~[jackson-databind-2.9.6.jar:2.9.6]
        at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1396) ~[jackson-databind-2.9.6.jar:2.9.6]
        at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:913) ~[jackson-databind-2.9.6.jar:2.9.6]
        at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:286) ~[spring-web-4.3.18.RELEASE.jar:4.3.18.RELEASE]
        ... 58 common frames omitted</p>
  </blockquote>
  
  <p>kvService only returns data from redis. It parses Hash data type (<a href="https://redis.io/topics/data-types" rel="noreferrer">https://redis.io/topics/data-types</a>) to Hash object defined in proto. Where Hash->category is main key of hash and values in hash redis datatype are converted to KVPair defined in proto. I cannot show all source code, because it calls other systems and source code is very long.</p>
  
  <p>kvService returns valid Hash object, but exception happens when I return this Hash object and spring tries convert it to JSON.</p>
  
  <p>important dependencies in my build.gradle:</p>
  
  <pre><code>def versions = [
          logback: '1.2.3',
          owner: '1.0.10',
          jackson: '2.9.6',
  
          guava: '25.1-jre',
          guice: '4.2.0',
          grpc: '1.9.1',
          protoc: '3.5.1',
  
          redis: '2.9.0',
  ]
  </code></pre>
  
  <p>dependencies {</p>
  
  <pre><code>compile group: 'ch.qos.logback', name: 'logback-classic', version: versions.logback
  compile group: 'org.aeonbits.owner', name: 'owner', version: versions.owner
  
  compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: versions.jackson
  compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: versions.jackson
  compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: versions.jackson
  
  compile group: 'com.google.guava', name: 'guava', version: versions.guava
  compile group: 'com.google.inject', name: 'guice', version: versions.guice
  compile group: 'io.grpc', name: 'grpc-netty', version: versions.grpc
  compile group: 'io.grpc', name: 'grpc-protobuf', version: versions.grpc
  compile group: 'io.grpc', name: 'grpc-stub', version: versions.grpc
  compile 'org.glassfish:javax.annotation:10.0-b28'
  
  
  compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.2.1'
  compile group: 'javax.activation', name: 'activation', version: '1.1.1'
  
  compile group: 'redis.clients', name: 'jedis', version: versions.redis
  </code></pre>
  
  <p>}</p>
  
  <p>As you can see in my protobuf definition isn´t any self-referencing. </p>
  
  <p>Is there any possible way to fix this problem ?</p>