Commit 21082226 by Thibaut Tropardy Committed by Johannes Edmeier

Add content-type application/json to the SlackNotifier

In case there are multuple MessageConverters present, we mus make sure that the message gets converted into json by setting the content-type header to application/json. fixes #445
parent 6d5b0f37
......@@ -8,6 +8,9 @@ import java.util.Map;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.event.ClientApplicationEvent;
......@@ -62,7 +65,7 @@ public class SlackNotifier extends AbstractStatusChangeNotifier {
this.restTemplate = restTemplate;
}
protected Object createMessage(ClientApplicationEvent event) {
protected HttpEntity<Map<String, Object>> createMessage(ClientApplicationEvent event) {
Map<String, Object> messageJson = new HashMap<>();
messageJson.put("username", username);
if (icon != null) {
......@@ -77,7 +80,10 @@ public class SlackNotifier extends AbstractStatusChangeNotifier {
attachments.put("color", getColor(event));
attachments.put("mrkdwn_in", Collections.singletonList("text"));
messageJson.put("attachments", Collections.singletonList(attachments));
return messageJson;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new HttpEntity<>(messageJson, headers);
}
protected String getText(ClientApplicationEvent event) {
......
......@@ -14,6 +14,9 @@ import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
......@@ -121,7 +124,7 @@ public class SlackNotifierTest {
infoDown, infoUp);
}
private Object expectedMessage(String color, String user, @Nullable String icon,
private HttpEntity<Map<String, Object>> expectedMessage(String color, String user, @Nullable String icon,
@Nullable String channel, String message) {
Map<String, Object> messageJson = new HashMap<>();
messageJson.put("username", user);
......@@ -139,7 +142,9 @@ public class SlackNotifierTest {
messageJson.put("attachments", Collections.singletonList(attachments));
return messageJson;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new HttpEntity<>(messageJson, headers);
}
private String standardMessage(String status, String appName, String id) {
......
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