Commit 63612117 by clalleme Committed by Johannes Stelzer

Add JSON content type when registering to Admin Server

fixes #62
parent d3f9728a
...@@ -15,9 +15,14 @@ ...@@ -15,9 +15,14 @@
*/ */
package de.codecentric.boot.admin.services; package de.codecentric.boot.admin.services;
import java.util.Collections;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
...@@ -32,6 +37,14 @@ public class SpringBootAdminRegistrator { ...@@ -32,6 +37,14 @@ public class SpringBootAdminRegistrator {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringBootAdminRegistrator.class); private static final Logger LOGGER = LoggerFactory.getLogger(SpringBootAdminRegistrator.class);
private static HttpHeaders HTTP_HEADERS = createHttpHeaders();
private AdminClientProperties clientProps;
private AdminProperties adminProps;
private final RestTemplate template;
public SpringBootAdminRegistrator(RestTemplate template, AdminProperties adminProps, public SpringBootAdminRegistrator(RestTemplate template, AdminProperties adminProps,
AdminClientProperties clientProps) { AdminClientProperties clientProps) {
this.clientProps = clientProps; this.clientProps = clientProps;
...@@ -39,11 +52,12 @@ public class SpringBootAdminRegistrator { ...@@ -39,11 +52,12 @@ public class SpringBootAdminRegistrator {
this.template = template; this.template = template;
} }
private AdminClientProperties clientProps; private static HttpHeaders createHttpHeaders() {
HttpHeaders headers = new HttpHeaders();
private AdminProperties adminProps; headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
private final RestTemplate template; return HttpHeaders.readOnlyHttpHeaders(headers);
}
/** /**
* Registers the client application at spring-boot-admin-server. * Registers the client application at spring-boot-admin-server.
...@@ -54,7 +68,8 @@ public class SpringBootAdminRegistrator { ...@@ -54,7 +68,8 @@ public class SpringBootAdminRegistrator {
String adminUrl = adminProps.getUrl() + '/' + adminProps.getContextPath(); String adminUrl = adminProps.getUrl() + '/' + adminProps.getContextPath();
try { try {
ResponseEntity<Application> response = template.postForEntity(adminUrl, app, Application.class); ResponseEntity<Application> response = template.postForEntity(adminUrl,
new HttpEntity<Application>(app, HTTP_HEADERS), Application.class);
if (response.getStatusCode().equals(HttpStatus.CREATED)) { if (response.getStatusCode().equals(HttpStatus.CREATED)) {
LOGGER.debug("Application registered itself as {}", response.getBody()); LOGGER.debug("Application registered itself as {}", response.getBody());
......
...@@ -23,8 +23,13 @@ import static org.mockito.Mockito.mock; ...@@ -23,8 +23,13 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.util.Collections;
import org.junit.Test; import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
...@@ -45,15 +50,23 @@ public class SpringBootAdminRegistratorTest { ...@@ -45,15 +50,23 @@ public class SpringBootAdminRegistratorTest {
clientProps.setName("AppName"); clientProps.setName("AppName");
RestTemplate restTemplate = mock(RestTemplate.class); RestTemplate restTemplate = mock(RestTemplate.class);
when(restTemplate.postForEntity(isA(String.class), isA(Application.class), eq(Application.class))).thenReturn( when(
new ResponseEntity<Application>(HttpStatus.CREATED)); restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class),
eq(Application.class))).thenReturn(
new ResponseEntity<Application>(HttpStatus.CREATED));
SpringBootAdminRegistrator registrator = new SpringBootAdminRegistrator(restTemplate, adminProps, clientProps); SpringBootAdminRegistrator registrator = new SpringBootAdminRegistrator(restTemplate, adminProps, clientProps);
boolean result = registrator.register(); boolean result = registrator.register();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
assertTrue(result); assertTrue(result);
verify(restTemplate).postForEntity("http://sba:8080/api/applications", verify(restTemplate).postForEntity("http://sba:8080/api/applications",
new Application("http://localhost:8080", "AppName"), Application.class); new HttpEntity<Application>(new Application("http://localhost:8080",
"AppName"),
headers), Application.class);
} }
@Test @Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment