我正在使用具体的定义与protobufs。
message Hash {
string category = 1;
repeated KVPair content = 2;
}
message KVPair {
string key = 1;
string value = 2;
}
我希望将这个Spring Boot应用程序发送为JSON格式。我已经将以下包添加到我的Gradle依赖项中:
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.6.1'
当我尝试使用以下代码输出生成的哈希对象时:
@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;
}
}
它抛出了一个致命错误异常:
``` 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 ```
kvService仅从redis返回数据。它解析哈希数据类型(https://redis.io/topics/data-types)到proto中定义的哈希对象。其中,哈希->类别是哈希的主键,哈希redis数据类型中的值转换为proto中定义的KVPair。我无法展示所有源代码,因为它调用其他系统且源代码非常长。
kvService返回有效的哈希对象,但当我返回此哈希对象并且Spring尝试将其转换为JSON时,会发生异常。
我的build.gradle中的重要依赖项:
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',
]
依赖关系 {
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
正如您在我的protobuf定义中所看到的,没有任何自我引用。
是否有可能解决这个问题?