How to return plain text in spring controller?

扶醉桌前 提交于 2019-12-10 04:51:41

问题


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)?


回答1:


If you remove produces="text/plain" from the mapping, it returns plain text but the header is set to "application/xml". This is probably not desirable. I tested with the latest version of Spring Boot.

If you are using the version of Spring >= 4.1.2, you can try to use defaultContentTypeStrategy instead of defaultContentType, to set the correct content type in the header:

   configurer.favorPathExtension(false)
            .favorParameter(true)
            .ignoreAcceptHeader(true)
            .useJaf(false)
            .defaultContentTypeStrategy(new ContentNegotiationStrategy() {
                @Override
                public List<MediaType> resolveMediaTypes(NativeWebRequest nativeWebRequest) throws
                        HttpMediaTypeNotAcceptableException {
                    System.out.println("Description:"+nativeWebRequest.getDescription(false));
                    if (nativeWebRequest.getDescription(false).endsWith("/test/my")) {
                        return Collections.singletonList(MediaType.TEXT_PLAIN);
                    }
                    else {
                        return Collections.singletonList(MediaType.APPLICATION_XML);
                    }
                }
            })
            //.defaultContentType(MediaType.APPLICATION_XML)
;



回答2:


If I recall well, you can annotate the method with @ResponseBody and it will return plain text.

Check this




回答3:


You need to specify @ResponseBody annotation for your test method like:

public @ResponseBody String test(){}


来源:https://stackoverflow.com/questions/33415036/how-to-return-plain-text-in-spring-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!