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-projects-issues opened this issue Dec 7, 2018 · 2 comments
Labels
for: stackoverflow in: core status: invalid

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues 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

@spring-projects-issues spring-projects-issues added type: bug status: waiting-for-triage in: core and removed type: bug labels Jan 11, 2019
@bclozel
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

@bclozel bclozel closed this as completed Jan 21, 2019
@bclozel bclozel added status: invalid for: stackoverflow and removed status: waiting-for-triage labels Jan 21, 2019
@fcabouat
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
Labels
for: stackoverflow in: core status: invalid
Projects
None yet
Development

No branches or pull requests

3 participants