我想返回一个简单的纯文本字符串,如下所示:
@RestController
@RequestMapping("/test")
public class TestController {
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/my", method = RequestMethod.GET, produces="text/plain")
public String test() {
return "OK";
}
Run Code Online (Sandbox Code Playgroud)
问题:我也有一个全局的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);
}
}
Run Code Online (Sandbox Code Playgroud)
结果:每当我访问spring控制器时,都会收到错误消息:
Could not find acceptable representation
问题:即使在内容协商中仅配置了XML(我必须保留),如何强制控制器返回纯文本?