我想返回一个简单的纯文本字符串,如下所示:
@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);
}
}
结果:每当我访问弹簧控制器时,我都会收到错误消息:
Could not find acceptable representation
问题:如何强制控制器返回纯文本,即使在内容协商中只配置了 XML(我必须保留)?