org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter Java Examples

The following examples show how to use org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MvcConfig.java    From tutorials with MIT License 6 votes vote down vote up
@Override
public void configureMessageConverters(final List<HttpMessageConverter<?>> messageConverters) {
    final Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.indentOutput(true)
        .dateFormat(new SimpleDateFormat("dd-MM-yyyy hh:mm"));
    messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build()));
    messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true)
        .build()));

    messageConverters.add(createXmlHttpMessageConverter());
    // messageConverters.add(new MappingJackson2HttpMessageConverter());

    messageConverters.add(new ProtobufHttpMessageConverter());
    messageConverters.add(new KryoHttpMessageConverter());
    messageConverters.add(new StringHttpMessageConverter());
}
 
Example #2
Source File: RestConfig.java    From kbear with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private ProtobufHttpMessageConverter newCustomProtobufMessageConverter() throws Exception {
    Constructor[] constructors = ProtobufHttpMessageConverter.class.getDeclaredConstructors();
    Constructor requiredConstructor = null;
    for (Constructor constructor : constructors) {
        if (constructor.getParameterTypes().length == 2) {
            constructor.setAccessible(true);
            requiredConstructor = constructor;
            break;
        }
    }

    Class[] classes = ProtobufHttpMessageConverter.class.getDeclaredClasses();
    Class requiredClass = null;
    for (Class clazz : classes) {
        if (clazz.getSimpleName().equals("ProtobufJavaUtilSupport")) {
            requiredClass = clazz;
            break;
        }
    }

    Constructor pbUtilSupportConstructor = requiredClass.getConstructor(Parser.class, Printer.class);
    pbUtilSupportConstructor.setAccessible(true);

    Parser parser = JsonFormat.parser().ignoringUnknownFields();
    Printer printer = JsonFormat.printer().includingDefaultValueFields().preservingProtoFieldNames()
            .omittingInsignificantWhitespace();
    Object support = pbUtilSupportConstructor.newInstance(parser, printer);
    return (ProtobufHttpMessageConverter) requiredConstructor.newInstance(support, null);
}
 
Example #3
Source File: RestConfig.java    From kbear with Apache License 2.0 5 votes vote down vote up
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.defaultContentType(MediaType.APPLICATION_JSON)
            .strategies(Arrays.asList(new ContentNegotiationStrategy() {
                @Override
                public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest)
                        throws HttpMediaTypeNotAcceptableException {
                    MediaType mediaType = MediaType.APPLICATION_JSON;
                    String accept = webRequest.getHeader(HttpHeaders.ACCEPT);
                    if (accept == null)
                        return Arrays.asList(mediaType);

                    switch (accept) {
                        case APPLICATION_PROTOBUF_VALUE:
                            mediaType = ProtobufHttpMessageConverter.PROTOBUF;
                            break;
                        case MediaType.APPLICATION_JSON_VALUE:
                            mediaType = MediaType.APPLICATION_JSON;
                            break;
                        default:
                            mediaType = MediaType.APPLICATION_JSON;
                            break;
                    }

                    return Arrays.asList(mediaType);
                }
            }));
}
 
Example #4
Source File: ProtobufSpringEncoderTest.java    From spring-cloud-openfeign with Apache License 2.0 5 votes vote down vote up
private SpringEncoder newEncoder() {
	ObjectFactory<HttpMessageConverters> converters = new ObjectFactory<HttpMessageConverters>() {
		@Override
		public HttpMessageConverters getObject() throws BeansException {
			return new HttpMessageConverters(new ProtobufHttpMessageConverter());
		}
	};
	return new SpringEncoder(converters);
}
 
Example #5
Source File: SpringHttpMessageConvertersLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenConsumingProtobuf_whenReadingTheFoo_thenCorrect() {
    final String URI = BASE_URI + "foos/{id}";

    final RestTemplate restTemplate = new RestTemplate();
    restTemplate.setMessageConverters(Arrays.asList(new ProtobufHttpMessageConverter()));
    final HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(ProtobufHttpMessageConverter.PROTOBUF));
    final HttpEntity<String> entity = new HttpEntity<String>(headers);

    final ResponseEntity<FooProtos.Foo> response = restTemplate.exchange(URI, HttpMethod.GET, entity, FooProtos.Foo.class, "1");
    final FooProtos.Foo resource = response.getBody();

    assertThat(resource, notNullValue());
}
 
Example #6
Source File: Application.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
ProtobufHttpMessageConverter protobufHttpMessageConverter() {
    return new ProtobufHttpMessageConverter();
}
 
Example #7
Source File: Application.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
RestTemplate restTemplate(ProtobufHttpMessageConverter hmc) {
    return new RestTemplate(Arrays.asList(hmc));
}
 
Example #8
Source File: DemoApplicationTests.java    From spring-and-google-protocol-buffers with Apache License 2.0 4 votes vote down vote up
@Bean
ProtobufHttpMessageConverter protobufHttpMessageConverter() {
    return new ProtobufHttpMessageConverter();
}
 
Example #9
Source File: DemoApplicationTests.java    From spring-and-google-protocol-buffers with Apache License 2.0 4 votes vote down vote up
@Bean
RestTemplate restTemplate(ProtobufHttpMessageConverter hmc) {
    return new RestTemplate(Arrays.asList(hmc));
}
 
Example #10
Source File: DemoApplication.java    From spring-and-google-protocol-buffers with Apache License 2.0 4 votes vote down vote up
@Bean
ProtobufHttpMessageConverter protobufHttpMessageConverter() {
    return new ProtobufHttpMessageConverter();
}
 
Example #11
Source File: ClientApplication.java    From spring-cloud-contract-samples with Apache License 2.0 4 votes vote down vote up
@Bean
ProtobufHttpMessageConverter protobufHttpMessageConverter() {
	return new ProtobufHttpMessageConverter();
}
 
Example #12
Source File: ClientApplication.java    From spring-cloud-contract-samples with Apache License 2.0 4 votes vote down vote up
@Bean
RestTemplate restTemplate(ProtobufHttpMessageConverter hmc) {
	return new RestTemplate(Collections.singletonList(hmc));
}
 
Example #13
Source File: ProtoConfiguration.java    From spring-cloud-contract-samples with Apache License 2.0 4 votes vote down vote up
@Bean
ProtobufHttpMessageConverter protobufHttpMessageConverter() {
	return new ProtobufHttpMessageConverter();
}
 
Example #14
Source File: TitusProtobufHttpMessageConverter.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
@Inject
public TitusProtobufHttpMessageConverter() {
    this.delegate = new ProtobufHttpMessageConverter();
}
 
Example #15
Source File: WebConfig.java    From grpc-swagger with MIT License 4 votes vote down vote up
@Bean
ProtobufHttpMessageConverter protobufHttpMessageConverter() {
    return new ProtobufHttpMessageConverter();
}
 
Example #16
Source File: SpringEncoder.java    From spring-cloud-openfeign with Apache License 2.0 4 votes vote down vote up
@Override
public void encode(Object requestBody, Type bodyType, RequestTemplate request)
		throws EncodeException {
	// template.body(conversionService.convert(object, String.class));
	if (requestBody != null) {
		Collection<String> contentTypes = request.headers()
				.get(HttpEncoding.CONTENT_TYPE);

		MediaType requestContentType = null;
		if (contentTypes != null && !contentTypes.isEmpty()) {
			String type = contentTypes.iterator().next();
			requestContentType = MediaType.valueOf(type);
		}

		if (Objects.equals(requestContentType, MediaType.MULTIPART_FORM_DATA)) {
			this.springFormEncoder.encode(requestBody, bodyType, request);
			return;
		}
		else {
			if (bodyType == MultipartFile.class) {
				log.warn(
						"For MultipartFile to be handled correctly, the 'consumes' parameter of @RequestMapping "
								+ "should be specified as MediaType.MULTIPART_FORM_DATA_VALUE");
			}
		}

		for (HttpMessageConverter messageConverter : this.messageConverters
				.getObject().getConverters()) {
			FeignOutputMessage outputMessage;
			try {
				if (messageConverter instanceof GenericHttpMessageConverter) {
					outputMessage = checkAndWrite(requestBody, bodyType,
							requestContentType,
							(GenericHttpMessageConverter) messageConverter, request);
				}
				else {
					outputMessage = checkAndWrite(requestBody, requestContentType,
							messageConverter, request);
				}
			}
			catch (IOException | HttpMessageConversionException ex) {
				throw new EncodeException("Error converting request body", ex);
			}
			if (outputMessage != null) {
				// clear headers
				request.headers(null);
				// converters can modify headers, so update the request
				// with the modified headers
				request.headers(getHeaders(outputMessage.getHeaders()));

				// do not use charset for binary data and protobuf
				Charset charset;
				if (messageConverter instanceof ByteArrayHttpMessageConverter) {
					charset = null;
				}
				else if (messageConverter instanceof ProtobufHttpMessageConverter
						&& ProtobufHttpMessageConverter.PROTOBUF.isCompatibleWith(
								outputMessage.getHeaders().getContentType())) {
					charset = null;
				}
				else {
					charset = StandardCharsets.UTF_8;
				}
				request.body(Request.Body.encoded(
						outputMessage.getOutputStream().toByteArray(), charset));
				return;
			}
		}
		String message = "Could not write request: no suitable HttpMessageConverter "
				+ "found for request type [" + requestBody.getClass().getName() + "]";
		if (requestContentType != null) {
			message += " and content type [" + requestContentType + "]";
		}
		throw new EncodeException(message);
	}
}
 
Example #17
Source File: WebConfig.java    From grpc-swagger with MIT License 4 votes vote down vote up
@Bean
ProtobufHttpMessageConverter protobufHttpMessageConverter() {
    return new ProtobufHttpMessageConverter();
}