I want to return a simple plain text string as follows:
@RestController
@RequestMapping("/test")
public class TestController {
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/my", method = RequestMethod.GET, produces="text/plain")
public String test() {
return "OK";
}
Problem: I also have a global ContentNegotiation filter as follows:
@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);
}
}
Result: whenever I access the spring controller, I'm getting the error:
Could not find acceptable representation
Question: how can I force the controller to return plain text, even though only XML was configured within content negotation (which I have to keep)?
@GetMapping
(or@RequestMapping
) is used, as the name implies, for mapping a request to a controller method. It does nothing to enforcing the content type being returned (produces actually accepts an array of values so you can have different methods for the same URL producing different content, but it isn't enforcing anything). – Pastoralist