简体   繁体   English

如何为 spring-data-rest 设置默认媒体类型?

[英]How to set the default media type for spring-data-rest?

From RepositoryRestConfiguration I can see that setting spring.data.rest.default-media-type=application/json could change the default media type served by @RepositoryRestResource .RepositoryRestConfiguration我可以看到,设置spring.data.rest.default-media-type=application/json可以改变默认介质类型由服务@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?问题:由于此类deprecation ,设置/覆盖默认类型的正确方法是什么?

You can do this via RepositoryRestConfiguration or simply with a property in your application.properties.您可以通过RepositoryRestConfiguration或简单地使用 application.properties 中的属性来执行此操作。 See the documentation here .请参阅此处的文档。

The RepositoryRestConfiguration class is NOT deprecated.不推荐使用RepositoryRestConfiguration类。 There are methods within it that are deprecated.其中有些方法已被弃用。 The @SuppressWarnings("deprecation") annotation on the class does not mean that the class itself is deprecated.类上的@SuppressWarnings("deprecation")注释并不意味着类本身已被弃用。 That is simply an annotation used to tell an IDE to not display deprecation warnings in the IDE.这只是用于告诉 IDE 在 IDE 中不显示弃用警告的注释。

The easiest way to do this would be in application.properties.最简单的方法是在 application.properties 中。 However, you have the property name wrong.但是,您的属性名称错误。 You wouldn't set it as spring.data.rest.default-media-type .您不会将其设置为spring.data.rest.default-media-type The actual property it would expect is spring.data.rest.defaultMediaType .它期望的实际属性是spring.data.rest.defaultMediaType So in your application.properties, you could have:所以在你的 application.properties 中,你可以有:

spring.data.rest.defaultMediaType=application/json

With the RepositoryRestConfiguration , you could accomplish the same as such:使用RepositoryRestConfiguration ,您可以完成相同的操作:

@Configuration
class CustomRestMvcConfiguration {

  @Bean
  public RepositoryRestConfigurer repositoryRestConfigurer() {

    return new RepositoryRestConfigurerAdapter() {

      @Override
      public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.setDefaultMediaType(MediaType.APPLICATION_JSON);
      }
    };
  }
}
@Component public class CustomRestConfig implements RepositoryRestConfigurer { @Override public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { config.setDefaultMediaType(MediaType.APPLICATION_JSON); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM