如何为 spring-data-rest 设置默认媒体类型? [英] How to set the default media type for spring-data-rest?
问题描述
从 RepositoryRestConfiguration
我可以看到设置 spring.data.rest.default-media-type=application/json
可以改变 提供的默认媒体类型@RepositoryRestResource
.
From RepositoryRestConfiguration
I can see that setting spring.data.rest.default-media-type=application/json
could change the default media type served by @RepositoryRestResource
.
@SuppressWarnings("deprecation")
public class RepositoryRestConfiguration {
private MediaType defaultMediaType = MediaTypes.HAL_JSON;
}
问题:由于此类处于弃用
中,设置/覆盖默认类型的正确方法是什么?
Question: as this class is in deprecation
, what is the correct way to set/override the default type?
推荐答案
您可以通过 RepositoryRestConfiguration
或仅使用 application.properties 中的属性来完成此操作.请参阅文档此处一>.
You can do this via RepositoryRestConfiguration
or simply with a property in your application.properties. See the documentation here.
RepositoryRestConfiguration
类没有被弃用.其中有些方法已被弃用.类上的 @SuppressWarnings("deprecation")
注释并不意味着该类本身已被弃用.这只是用于告诉 IDE 在 IDE 中不显示弃用警告的注释.
The RepositoryRestConfiguration
class is NOT deprecated. There are methods within it that are deprecated. The @SuppressWarnings("deprecation")
annotation on the class does not mean that the class itself is deprecated. That is simply an annotation used to tell an IDE to not display deprecation warnings in the IDE.
最简单的方法是在 application.properties 中.但是,您的属性名称错误.您不会将其设置为 spring.data.rest.default-media-type
.它期望的实际属性是 spring.data.rest.defaultMediaType
.因此,在您的 application.properties 中,您可以:
The easiest way to do this would be in application.properties. However, you have the property name wrong. You wouldn't set it as spring.data.rest.default-media-type
. The actual property it would expect is spring.data.rest.defaultMediaType
. So in your application.properties, you could have:
spring.data.rest.defaultMediaType=application/json
使用 RepositoryRestConfiguration
,您可以完成相同的操作:
With the RepositoryRestConfiguration
, you could accomplish the same as such:
@Configuration
class CustomRestMvcConfiguration {
@Bean
public RepositoryRestConfigurer repositoryRestConfigurer() {
return new RepositoryRestConfigurerAdapter() {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setDefaultMediaType(MediaType.APPLICATION_JSON);
}
};
}
}
这篇关于如何为 spring-data-rest 设置默认媒体类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!