Ensure that, where appropriate, actuator endpoints always produce JSON

Previously, the Actuator’s endpoints did not specify a produces
attribute on their request mappings. With Jackson’s XML binding on the
classpath, this would lead to requests made by a browser receiving
application/xml responses (due to the Accept header indicating that
application/xml is preferred). This was problematic as some of the
response payloads were not legal xml. Problems included XML tags
beginning with ‘\’ or containing ‘#’.

This commit updates the endpoints to specify that they produce
application/json. The environment and metrics endpoints have also been
updated so that always return a JSON object, even when they are
returning a single entry. This consistency avoids problems where
clients may not consider a single scalar value to be legal JSON.

Closes gh-2449
pull/4259/head
Andy Wilkinson 9 years ago
parent cc3b7ca6f6
commit 2109559f37

@ -17,6 +17,7 @@
package org.springframework.boot.actuate.endpoint.mvc;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@ -38,7 +39,7 @@ public class EndpointMvcAdapter extends AbstractEndpointMvcAdapter<Endpoint<?>>
}
@Override
@RequestMapping(method = RequestMethod.GET)
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Object invoke() {
return super.invoke();

@ -24,6 +24,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySources;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@ -46,7 +47,7 @@ public class EnvironmentMvcEndpoint extends EndpointMvcAdapter
super(delegate);
}
@RequestMapping(value = "/{name:.*}", method = RequestMethod.GET)
@RequestMapping(value = "/{name:.*}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@HypermediaDisabled
public Object value(@PathVariable String name) {

@ -28,6 +28,7 @@ import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
@ -123,7 +124,7 @@ public class HealthMvcEndpoint extends AbstractEndpointMvcAdapter<HealthEndpoint
this.statusMapping.put(statusCode, httpStatus);
}
@RequestMapping
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Object invoke(Principal principal) {
if (!getDelegate().isEnabled()) {

@ -20,6 +20,7 @@ import java.util.Map;
import org.springframework.boot.actuate.endpoint.MetricsEndpoint;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@ -42,7 +43,7 @@ public class MetricsMvcEndpoint extends EndpointMvcAdapter {
this.delegate = delegate;
}
@RequestMapping(value = "/{name:.*}", method = RequestMethod.GET)
@RequestMapping(value = "/{name:.*}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@HypermediaDisabled
public Object value(@PathVariable String name) {

@ -16,6 +16,7 @@
package org.springframework.boot.actuate.endpoint.mvc;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Pattern;
@ -29,6 +30,7 @@ import java.util.regex.Pattern;
* @param <T> The source data type
* @author Phillip Webb
* @author Sergei Egorov
* @author Andy Wilkinson
* @since 1.3.0
*/
abstract class NamePatternFilter<T> {
@ -41,9 +43,12 @@ abstract class NamePatternFilter<T> {
this.source = source;
}
public Object getResults(String name) {
public Map<String, Object> getResults(String name) {
if (!isRegex(name)) {
return getValue(this.source, name);
Object value = getValue(this.source, name);
Map<String, Object> result = new HashMap<String, Object>();
result.put(name, value);
return result;
}
Pattern pattern = Pattern.compile(name);
ResultCollectingNameCallback resultCollector = new ResultCollectingNameCallback(

@ -41,7 +41,6 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -79,7 +78,7 @@ public class EnvironmentMvcEndpointTests {
@Test
public void sub() throws Exception {
this.mvc.perform(get("/env/foo")).andExpect(status().isOk())
.andExpect(content().string(equalToIgnoringCase("bar")));
.andExpect(content().string("{\"foo\":\"bar\"}"));
}
@Test

@ -87,7 +87,7 @@ public class MetricsMvcEndpointTests {
@Test
public void specificMetric() throws Exception {
this.mvc.perform(get("/metrics/foo")).andExpect(status().isOk())
.andExpect(content().string(equalTo("1")));
.andExpect(content().string(equalTo("{\"foo\":1}")));
}
@Test

@ -21,27 +21,29 @@ import java.util.Map;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link NamePatternFilter}.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
public class NamePatternFilterTests {
@Test
public void nonRegex() throws Exception {
MockNamePatternFilter filter = new MockNamePatternFilter();
assertThat(filter.getResults("not.a.regex"), equalTo((Object) "not.a.regex"));
assertThat(filter.getResults("not.a.regex"),
hasEntry("not.a.regex", (Object) "not.a.regex"));
assertThat(filter.isGetNamesCalled(), equalTo(false));
}
@Test
@SuppressWarnings("unchecked")
public void regex() throws Exception {
MockNamePatternFilter filter = new MockNamePatternFilter();
Map<String, Object> results = (Map<String, Object>) filter.getResults("fo.*");
Map<String, Object> results = filter.getResults("fo.*");
assertThat(results.get("foo"), equalTo((Object) "foo"));
assertThat(results.get("fool"), equalTo((Object) "fool"));
assertThat(filter.isGetNamesCalled(), equalTo(true));

Loading…
Cancel
Save