Add redirection to actuator endpoint

If a request to the actuator endpoint ends with a slash with Spring
HATEOAS on the classpath, an empty array of links is returned whereas
a request without the slash returns a response with all the expected
links to the actuator's other endpoints.

This commit adds an automatic redirection so that both URIs return the
expected result.

Closes gh-4575
This commit is contained in:
Stephane Nicoll 2015-11-30 10:32:50 +01:00
parent 5beeaf1760
commit 7d6f63ae34
3 changed files with 14 additions and 1 deletions

View File

@ -87,6 +87,11 @@ public class HalJsonMvcEndpoint extends WebMvcConfigurerAdapter
return new ResourceSupport();
}
@RequestMapping(path = "/", produces = MediaType.APPLICATION_JSON_VALUE)
public String forward() {
return "redirect:" + this.managementServletContext.getContextPath() + this.path;
}
public void setPath(String path) {
this.path = path;
}

View File

@ -39,6 +39,7 @@ import org.springframework.web.context.WebApplicationContext;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -75,6 +76,13 @@ public class HalBrowserMvcEndpointManagementContextPathIntegrationTests {
.andExpect(status().isOk()).andExpect(jsonPath("$._links").exists());
}
@Test
public void redirectJson() throws Exception {
this.mockMvc.perform(get("/admin/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isFound())
.andExpect(header().string("location", "/admin"));
}
@Test
public void actuatorHomeHtml() throws Exception {
this.mockMvc.perform(get("/admin/").accept(MediaType.TEXT_HTML))

View File

@ -124,7 +124,7 @@ public class MvcEndpointIntegrationTests {
"management.context-path:/management");
MockMvc mockMvc = createSecureMockMvc();
mockMvc.perform(get("/management/info")).andExpect(status().isOk());
mockMvc.perform(get("/management/")).andExpect(status().isOk());
mockMvc.perform(get("/management")).andExpect(status().isOk());
}
@Test