Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring RepositoryRestResource response only if specific headers exist

How do i can specify @RepositoryRestResource enpoints to response only if mime-type is application/json?

example with @RequestMapping

GET-Request with Accept : application/json returns json

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

GET-Request without Accept : application/json header returns html

@RequestMapping("/path" )
public String withoutHeader() {
    return  "<html>...</html>";
}
like image 451
nAku Avatar asked Mar 11 '26 23:03

nAku


1 Answers

You cannot make it out of the box. You need add a configuration like this

@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");
      }
    };
  }
}
like image 113
pvpkiran Avatar answered Mar 13 '26 12:03

pvpkiran