Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring Boot 2.x not converting Protobuf to JSON Using ProtobufJsonFormatHttpMessageConverter [SPR-17578] #22110

Closed
spring-issuemaster opened this issue Dec 7, 2018 · 2 comments

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Dec 7, 2018

Brajveer Singh opened SPR-17578 and commented

I am using ProtobufJsonFormatHttpMessageConverter in Springboot 2.0

@bean
ProtobufJsonFormatHttpMessageConverter protobufJsonFormatHttpMessageConverter() {
    JsonFormat.Printer printer = JsonFormat.printer().omittingInsignificantWhitespace();
    JsonFormat.Parser parser = JsonFormat.parser().ignoringUnknownFields();
    return new ProtobufJsonFormatHttpMessageConverter(parser,printer,(ExtensionRegistry)null);
}

While running application, it's not converting output to JSON rather its printing as Protobuf text as below.

����Chris�
Richardson*�
�[crichardson@email.com|mailto:crichardson@email.com]��


Affects: 5.1.3

@bclozel

This comment has been minimized.

Copy link
Member

@bclozel bclozel commented Jan 21, 2019

@BrajveerSingh Spring Framework has unit tests covering this. I've successfully serialized a protobuf message to JSON with the following:

JsonFormat.Printer printer = JsonFormat.printer().omittingInsignificantWhitespace();
JsonFormat.Parser parser = JsonFormat.parser().ignoringUnknownFields();
ProtobufJsonFormatHttpMessageConverter converter = new ProtobufJsonFormatHttpMessageConverter(parser, printer);
PersonProtos.Person chris = PersonProtos.Person.newBuilder()
		.setFirstName("Chris").setLastName("Richardson")
		.setEmail("crichardson@email.com").build();

MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
try {
	converter.write(chris, MediaType.APPLICATION_JSON, outputMessage);
	System.out.println(outputMessage.getBodyAsString());
	// prints {"firstName":"Chris","lastName":"Richardson","email":"crichardson@email.com"}
}
catch (IOException e) {
	e.printStackTrace();
}

I'm closing this issue as nothing shows that there's a bug in Spring Framework.
Please ask further questions on StackOverflow, or reopen this issue with a repro project I can clone and run that shows the issue.

Thanks

@fcabouat

This comment has been minimized.

Copy link

@fcabouat fcabouat commented Oct 21, 2019

(had the same issue, it was just that I forgot to content negotiate / produces = MediaType.APPLICATION_JSON_VALUE on my controller)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.