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; ...@@ -8,6 +8,9 @@ import java.util.Map;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext; import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser; 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 org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.event.ClientApplicationEvent; import de.codecentric.boot.admin.event.ClientApplicationEvent;
...@@ -62,7 +65,7 @@ public class SlackNotifier extends AbstractStatusChangeNotifier { ...@@ -62,7 +65,7 @@ public class SlackNotifier extends AbstractStatusChangeNotifier {
this.restTemplate = restTemplate; this.restTemplate = restTemplate;
} }
protected Object createMessage(ClientApplicationEvent event) { protected HttpEntity<Map<String, Object>> createMessage(ClientApplicationEvent event) {
Map<String, Object> messageJson = new HashMap<>(); Map<String, Object> messageJson = new HashMap<>();
messageJson.put("username", username); messageJson.put("username", username);
if (icon != null) { if (icon != null) {
...@@ -77,7 +80,10 @@ public class SlackNotifier extends AbstractStatusChangeNotifier { ...@@ -77,7 +80,10 @@ public class SlackNotifier extends AbstractStatusChangeNotifier {
attachments.put("color", getColor(event)); attachments.put("color", getColor(event));
attachments.put("mrkdwn_in", Collections.singletonList("text")); attachments.put("mrkdwn_in", Collections.singletonList("text"));
messageJson.put("attachments", Collections.singletonList(attachments)); 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) { protected String getText(ClientApplicationEvent event) {
......
...@@ -14,6 +14,9 @@ import javax.annotation.Nullable; ...@@ -14,6 +14,9 @@ import javax.annotation.Nullable;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; 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 org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent; import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
...@@ -121,7 +124,7 @@ public class SlackNotifierTest { ...@@ -121,7 +124,7 @@ public class SlackNotifierTest {
infoDown, infoUp); 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) { @Nullable String channel, String message) {
Map<String, Object> messageJson = new HashMap<>(); Map<String, Object> messageJson = new HashMap<>();
messageJson.put("username", user); messageJson.put("username", user);
...@@ -139,7 +142,9 @@ public class SlackNotifierTest { ...@@ -139,7 +142,9 @@ public class SlackNotifierTest {
messageJson.put("attachments", Collections.singletonList(attachments)); 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) { 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