如何更改类注释@RepositoryRestController中方法提供的http响应的内容类型头

时间:2018-04-06 15:32:14

标签: spring-boot content-type spring-data-rest spring-restcontroller

我上课了:

@RepositoryRestController
@RequestMapping(value = "users")
public class UserController  {

...other methods...

@GetMapping(value = "returnText", produces = MediaType.TEXT_PLAIN_VALUE)
    public String returnText() {
        return "my text";
    }
}

当Postman调用此方法时,http响应内容类型标题为 application / hal + json ,但我使用了produces = MediaType.TEXT_PLAIN_VALUE

我可以以某种方式更改响应的content-type标题吗?

@RepositoryRestController只能返回application/hal+json吗?

我想在另一种方法中使用@RepositoryRestController返回image/jpg contet-type,但由于同样的原因而无效。

我认为这将在@RestController类中起作用,但为什么要使用它,如果我的应用程序是Spring Data Rest并且我想为我的控制器使用@RepositoryRestController注释。

1 个答案:

答案 0 :(得分:0)

来自SDR documentation

  

...利用Spring Data REST的设置,消息转换器,异常处理等,使用@RepositoryRestController注释......

     

如果您对特定于实体的操作不感兴趣,但仍希望在 basePath 下构建自定义操作,例如Spring MVC视图,资源等,请使用 @BasePathAwareController

因此,请尝试使用@BasePathAwareController代替@RepositoryRestController