这是我的控制器类:
@Controller
@RequestMapping("/actuator")
public class HealthController {
@RequestMapping(value = "/metrics", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON)
@ResponseBody
public HealthModel getDump() throws JsonProcessingException {
return new HealthModel();
//return mapper.writeValueAsString(metrics.invoke());
}
@RequestMapping(value = "/metrics", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN)
@ResponseBody
public String getHealth() {
return "HEALTHY";
}
}
模型
public class HealthModel {
@JsonProperty
private String status;
@JsonProperty
private int id;
public HealthModel(){
this.status="WARN";
this.id=2;
}
}
注意我已经映射/metrics
到返回json
或plain-text
取决于Accept Header
请求中的
当我在
curl -v -H "Accept: application/json" http://localhost:8080/myapp/actuator/metrics
我在 json 中得到预期的响应
{"status":"WARN","id":2}
。
但是,当我尝试
curl -v -H "Accept: text/plain" http://localhost:8080/myapp/actuator/metrics
我明白了HTTP/1.1 406 Not Acceptable
。
编辑
@EnableWebMvc
@Configuration
public class AppMvcConfig extends WebMvcConfigurerAdapter {
@Resource(name = "appObjectMapper")
private ObjectMapper appObjectMapper;
@Resource(name = "modelObjectMapper")
private ObjectMapper modelObjectMapper;
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
final MappingJackson2HttpMessageConverter inputConverter = new MappingJackson2HttpMessageConverter();
inputConverter.setObjectMapper(appObjectMapper);
final MappingJackson2HttpMessageConverter outputConverter = new MappingJackson2HttpMessageConverter();
outputConverter.setObjectMapper(modelObjectMapper);
converters.add(new JacksonDualMapperConverter(appObjectMapper, modelObjectMapper));
super.configureMessageConverters(converters);
}
}