From 0670a36ef2d809b9cfd5a56ccc4d040bcbb3fec0 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 14 Feb 2018 10:40:07 -0500 Subject: [PATCH] Polishing DefaultSchemaRegistryClient - made RestTemplate final - removed setter Resolves #1222 #1223 --- .../client/DefaultSchemaRegistryClient.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/client/DefaultSchemaRegistryClient.java b/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/client/DefaultSchemaRegistryClient.java index c3f81606c..5ab12a093 100644 --- a/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/client/DefaultSchemaRegistryClient.java +++ b/spring-cloud-stream-schema/src/main/java/org/springframework/cloud/stream/schema/client/DefaultSchemaRegistryClient.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,7 +31,7 @@ import org.springframework.web.client.RestTemplate; */ public class DefaultSchemaRegistryClient implements SchemaRegistryClient { - private RestTemplate template; + private RestTemplate restTemplate; private String endpoint = "http://localhost:8990"; @@ -40,8 +40,8 @@ public class DefaultSchemaRegistryClient implements SchemaRegistryClient { } public DefaultSchemaRegistryClient(RestTemplate restTemplate) { - Assert.notNull(restTemplate,"restTemplate cannot be null."); - this.template = restTemplate; + Assert.notNull(restTemplate,"'restTemplate' must not be null."); + this.restTemplate = restTemplate; } protected String getEndpoint() { @@ -54,21 +54,17 @@ public class DefaultSchemaRegistryClient implements SchemaRegistryClient { } protected RestTemplate getRestTemplate() { - return this.template; - } - - public void setRestTemplate(RestTemplate restTemplate) { - Assert.notNull(restTemplate,"restTemplate cannot be null."); - this.template = restTemplate; + return this.restTemplate; } + @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public SchemaRegistrationResponse register(String subject, String format, String schema) { Map requestBody = new HashMap<>(); requestBody.put("subject", subject); requestBody.put("format", format); requestBody.put("definition", schema); - ResponseEntity responseEntity = this.template.postForEntity(this.endpoint, requestBody, Map.class); + ResponseEntity responseEntity = this.restTemplate.postForEntity(this.endpoint, requestBody, Map.class); if (responseEntity.getStatusCode().is2xxSuccessful()) { SchemaRegistrationResponse registrationResponse = new SchemaRegistrationResponse(); Map responseBody = (Map) responseEntity.getBody(); @@ -81,9 +77,10 @@ public class DefaultSchemaRegistryClient implements SchemaRegistryClient { throw new RuntimeException("Failed to register schema: " + responseEntity.toString()); } + @SuppressWarnings("rawtypes") @Override public String fetch(SchemaReference schemaReference) { - ResponseEntity responseEntity = this.template.getForEntity( + ResponseEntity responseEntity = this.restTemplate.getForEntity( this.endpoint + "/" + schemaReference.getSubject() + "/" + schemaReference .getFormat() + "/v" + schemaReference .getVersion(), @@ -94,9 +91,10 @@ public class DefaultSchemaRegistryClient implements SchemaRegistryClient { return (String) responseEntity.getBody().get("definition"); } + @SuppressWarnings("rawtypes") @Override public String fetch(int id) { - ResponseEntity responseEntity = this.template.getForEntity( + ResponseEntity responseEntity = this.restTemplate.getForEntity( this.endpoint + "/schemas/" + id, Map.class); if (!responseEntity.getStatusCode().is2xxSuccessful()) { throw new RuntimeException("Failed to fetch schema: " + responseEntity.toString());