升级到Spring Boot 1.5-如何重构RepositoryRestMvcConfiguration?

时间:2018-07-31 14:36:15

标签: java spring rest spring-boot configuration

升级到Spring Boot 1.5后,以下代码将无法编译。显然,这两个方法(configureRepositoryRestConfigurationconfigureValidatingRepositoryEventListener)已从RepositoryRestMvcConfiguration类中删除。我还没有找到任何有关通过Java在Spring Boot 1.5中配置rest控制器的“正确”方法的文档。我在Spring Boot 1.5+上找到的所有指南都涉及基于XML的配置,而我会强烈更喜欢坚持使用基于Java的配置。如何重构该类以使其与1.5更改兼容?

@Configuration
public class CustomRepositoryRestMvcConfiguration extends RepositoryRestMvcConfiguration {

  private static final String BASE_URI = "/rest";
  @Autowired private BeforeCreateSnippetValidator beforeCreateSnippetValidator;
  @Autowired private BeforeCreateModuleValidator beforeCreateModuleValidator;

  @Override
  protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
    super.configureRepositoryRestConfiguration(config);
    config.setBasePath(BASE_URI);
    config.exposeIdsFor(Snippet.class, Placement.class, Module.class, Page.class, Position.class);
  }

  @Override
  protected void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener v) {
    v.addValidator("beforeCreate", beforeCreateSnippetValidator);
    v.addValidator("beforeCreate", beforeCreateModuleValidator);
  }
}

1 个答案:

答案 0 :(得分:0)

最后 ,经过很多猜测,我找到了解决方案。您只需要将RepositoryRestMvcConfiguration更改为RepositoryRestConfigurerAdapter,并将@Configuration更改为@Component。这是假设您已经进行了组件扫描设置。

@Component
public class CustomRepositoryRestMvcConfiguration extends RepositoryRestConfigurerAdapter {

  private static final String BASE_URI = "/rest";
  @Autowired private BeforeCreateSnippetValidator beforeCreateSnippetValidator;
  @Autowired private BeforeCreateModuleValidator beforeCreateModuleValidator;

  @Override
  public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
    super.configureRepositoryRestConfiguration(config);
    config.setBasePath(BASE_URI);
    config.exposeIdsFor(Snippet.class, Placement.class, Module.class, Page.class, Position.class);
  }

  @Override
  public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener v) {
    v.addValidator("beforeCreate", beforeCreateSnippetValidator);
    v.addValidator("beforeCreate", beforeCreateModuleValidator);
  }
}