仅当存在特定标头时,Spring RepositoryRestResource响应

时间:2018-01-31 11:39:58

标签: java json spring rest spring-boot

如果mime-type为@RepositoryRestResource,我如何指定application/json个enpoints来响应?

@RequestMapping

的示例

GET-Request Accept : application/json返回json

 @RequestMapping(path="/path", headers ="Accept=application/json")
    public String withHeader() {
        return  "{this:json}";
    }

没有Accept : application/json标头的GET-Request返回html

@RequestMapping("/path" )
public String withoutHeader() {
    return  "<html>...</html>";
}

1 个答案:

答案 0 :(得分:1)

你无法开箱即用。您需要添加这样的配置

@Configuration
class RestMvcConfiguration {

  @Bean
  public RepositoryRestConfigurer repositoryRestConfigurer() {

    return new RepositoryRestConfigurerAdapter() {

      @Override
      public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.returnBodyOnUpdate("Accept=application/json")
        config.returnBodyOnCreate("Accept=application/json");
      }
    };
  }
}