【问题标题】:How to return plain text in spring controller?如何在spring控制器中返回纯文本?
【发布时间】: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


    【解决方案1】:

    如果我没记错的话,您可以使用 @ResponseBody 注释该方法,它会返回纯文本。

    Check this

    【讨论】:

      【解决方案2】:

      您需要为您的测试方法指定@ResponseBody 注释,例如:

      public @ResponseBody String test(){}
      

      【讨论】:

      • 不,@RestController 暗示 @ResponseBody
      • 就像@membersound 提到的那样,使用@RestController 相当于在控制器中的所有请求映射方法上写@ResponseBody
      【解决方案3】:

      如果您从映射中删除 produces="text/plain",它会返回纯文本,但标头设置为 "application/xml"。这可能是不可取的。 我使用最新版本的 Spring Boot 进行了测试。

      如果你使用的是Spring >= 4.1.2的版本,你可以尝试使用defaultContentTypeStrategy而不是defaultContentType,在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)
      ;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-16
        • 1970-01-01
        • 2015-11-12
        • 2020-01-18
        • 1970-01-01
        • 2014-12-14
        相关资源
        最近更新 更多