如何指定 @RepositoryRestResource
仅在 mime-type 为 application/json
时指定响应?
@RequestMapping
示例
GET-Request with Accept : application/json
返回 json
@RequestMapping(path="/path", headers ="Accept=application/json")
public String withHeader() {
return "{this:json}";
}
没有 Accept : application/json
header 的 GET-Request 返回 html
@RequestMapping("/path" )
public String withoutHeader() {
return "<html>...</html>";
}
最佳答案
您无法开箱即用。您需要添加这样的配置
@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");
}
};
}
}
关于java - 仅当特定 header 存在时,Spring RepositoryRestResource 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48541279/