【发布时间】:2016-01-29 15:24:51
【问题描述】:
我想返回一个简单的纯文本字符串如下:
@RestController
@RequestMapping("/test")
public class TestController {
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/my", method = RequestMethod.GET, produces="text/plain")
public String test() {
return "OK";
}
问题:我还有一个全局 ContentNegotiation 过滤器,如下所示:
@Configuration
public class ContentNegotiationAdapter extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false)
.favorParameter(true)
.ignoreAcceptHeader(true)
.useJaf(false)
.defaultContentType(MediaType.APPLICATION_XML);
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
}
}
结果:每当我访问 spring 控制器时,我都会收到错误消息:
Could not find acceptable representation
问题:如何强制控制器返回纯文本,即使在内容协商中仅配置了 XML(我必须保留)?
【问题讨论】:
标签: spring plaintext spring-restcontroller