Skip to content
This repository was archived by the owner on Jun 29, 2018. It is now read-only.

Commit 7ad1142

Browse files
Johannes StelzerJohannes Stelzer
authored andcommitted
Merge branch 'ouaibsky-master'
2 parents d3f9728 + 6361211 commit 7ad1142

2 files changed

Lines changed: 37 additions & 9 deletions

File tree

spring-boot-admin-starter-client/src/main/java/de/codecentric/boot/admin/services/SpringBootAdminRegistrator.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
*/
1616
package de.codecentric.boot.admin.services;
1717

18+
import java.util.Collections;
19+
1820
import org.slf4j.Logger;
1921
import org.slf4j.LoggerFactory;
22+
import org.springframework.http.HttpEntity;
23+
import org.springframework.http.HttpHeaders;
2024
import org.springframework.http.HttpStatus;
25+
import org.springframework.http.MediaType;
2126
import org.springframework.http.ResponseEntity;
2227
import org.springframework.web.client.RestTemplate;
2328

@@ -32,18 +37,27 @@ public class SpringBootAdminRegistrator {
3237

3338
private static final Logger LOGGER = LoggerFactory.getLogger(SpringBootAdminRegistrator.class);
3439

40+
private static HttpHeaders HTTP_HEADERS = createHttpHeaders();
41+
42+
private AdminClientProperties clientProps;
43+
44+
private AdminProperties adminProps;
45+
46+
private final RestTemplate template;
47+
3548
public SpringBootAdminRegistrator(RestTemplate template, AdminProperties adminProps,
3649
AdminClientProperties clientProps) {
3750
this.clientProps = clientProps;
3851
this.adminProps = adminProps;
3952
this.template = template;
4053
}
4154

42-
private AdminClientProperties clientProps;
43-
44-
private AdminProperties adminProps;
45-
46-
private final RestTemplate template;
55+
private static HttpHeaders createHttpHeaders() {
56+
HttpHeaders headers = new HttpHeaders();
57+
headers.setContentType(MediaType.APPLICATION_JSON);
58+
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
59+
return HttpHeaders.readOnlyHttpHeaders(headers);
60+
}
4761

4862
/**
4963
* Registers the client application at spring-boot-admin-server.
@@ -54,7 +68,8 @@ public boolean register() {
5468
String adminUrl = adminProps.getUrl() + '/' + adminProps.getContextPath();
5569

5670
try {
57-
ResponseEntity<Application> response = template.postForEntity(adminUrl, app, Application.class);
71+
ResponseEntity<Application> response = template.postForEntity(adminUrl,
72+
new HttpEntity<Application>(app, HTTP_HEADERS), Application.class);
5873

5974
if (response.getStatusCode().equals(HttpStatus.CREATED)) {
6075
LOGGER.debug("Application registered itself as {}", response.getBody());

spring-boot-admin-starter-client/src/test/java/de/codecentric/boot/admin/services/SpringBootAdminRegistratorTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@
2323
import static org.mockito.Mockito.verify;
2424
import static org.mockito.Mockito.when;
2525

26+
import java.util.Collections;
27+
2628
import org.junit.Test;
29+
import org.springframework.http.HttpEntity;
30+
import org.springframework.http.HttpHeaders;
2731
import org.springframework.http.HttpStatus;
32+
import org.springframework.http.MediaType;
2833
import org.springframework.http.ResponseEntity;
2934
import org.springframework.web.client.RestClientException;
3035
import org.springframework.web.client.RestTemplate;
@@ -45,15 +50,23 @@ public void register_successful() {
4550
clientProps.setName("AppName");
4651

4752
RestTemplate restTemplate = mock(RestTemplate.class);
48-
when(restTemplate.postForEntity(isA(String.class), isA(Application.class), eq(Application.class))).thenReturn(
49-
new ResponseEntity<Application>(HttpStatus.CREATED));
53+
when(
54+
restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class),
55+
eq(Application.class))).thenReturn(
56+
new ResponseEntity<Application>(HttpStatus.CREATED));
5057

5158
SpringBootAdminRegistrator registrator = new SpringBootAdminRegistrator(restTemplate, adminProps, clientProps);
5259
boolean result = registrator.register();
5360

61+
HttpHeaders headers = new HttpHeaders();
62+
headers.setContentType(MediaType.APPLICATION_JSON);
63+
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
64+
5465
assertTrue(result);
5566
verify(restTemplate).postForEntity("http://sba:8080/api/applications",
56-
new Application("http://localhost:8080", "AppName"), Application.class);
67+
new HttpEntity<Application>(new Application("http://localhost:8080",
68+
"AppName"),
69+
headers), Application.class);
5770
}
5871

5972
@Test

0 commit comments

Comments
 (0)