Spring Data Rest @RepositoryRestController和@RequestMapping

时间:2017-11-23 04:53:01

标签: java spring rest spring-mvc spring-data-rest

我想使用设置了SDR基本路径的@RepositoryRestController来覆盖@RepositoryRestResource自动生成的控制器方法  到“/ api”。

Spring Data Rest 3.0(及更早版本)说:

“此控制器[如代码段中所示]将从RepositoryRestConfiguration.setBasePath中定义的相同API基本路径提供,该路径由所有其他RESTful端点(例如/ api)使用”。 https://docs.spring.io/spring-data/rest/docs/3.0.1.RELEASE/reference/html/#customizing-sdr.overriding-sdr-response-handlers(第15.4章)

但是,此代码段在类级别上没有@RequestMapping

我的SDR应用程序配置了RepositoryRestConfiguration对象

config.setBasePath("/api");

@RepositoryRestController未覆盖SDR的自动生成控制器方法。

请考虑接受此帖子的安装: Spring Data Rest controllers: behaviour and usage of @BasePathAwareController, @RepositoryRestController, @Controller and @RestController

请帮我理解这个! :)

AppConf.java:

@Configuration
@Import(value = {DataConf.class})
@EnableWebMvc
@ComponentScan(value = "pl.mydomain.controller")
public class AppConf
{
    @Bean
    public RepositoryRestConfigurer repositoryRestConfigurer() {
        return new RepositoryRestConfigurerAdapter() {
            public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
                config.setBasePath("/api");
            }
        };
    }
}

TokenController.java:

@RepositoryRestController
public class TokenController
{
    private TokenRepository repository;

    @Autowired
    public TokenController(TokenRepository tokenRepository) {
        this.repository = tokenRepository;
    }

    @RequestMapping(method = GET, path = "/tokens")
    public @ResponseBody ResponseEntity<?> tokens() 
    {    
        return ResponseEntity.ok("Hello");
    }
}

TokenRepository.java:

@RepositoryRestResource(path = "tokens")
public interface TokenRepository extends CrudRepository<Token, Long>{
}

1 个答案:

答案 0 :(得分:1)

解决上述困境的关键是以正确的方式配置项目。也就是说,将@ComponentScan放在传递给AbstractAnnotationConfigDispatcherServletInitializer::getServletConfigClasses()方法的类中(而不是传递给getRootConfigClasses()的AppConf.java)。

DispatcherConf.java:

public class DispatcherConf extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {AppConf.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] {WebConf.class}; // !!!
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"/*"};
    }
}

AppConf.java:

@Configuration
@Import({DataConf.class})
public class ApplicationConf
{
    @Bean
    public RepositoryRestConfigurer repositoryRestConfigurer() {
        return new RepositoryRestConfigurerAdapter() {
            @Override
            public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
                 config.setBasePath("/api"); // !!!
            }
        };
    }
}

DataConf.java:

@Configuration
@EnableJpaRepositories(basePackages = {
        "pl.example.data.repository"
})
@EnableTransactionManagement
public class DataConf
{ ... }

WebConf.java:

@Import(RepositoryRestMvcConfiguration.class)
@ComponentScan({"pl.example.api.controller"}) // !!!
public class WebConf {
}

即使我解开了谜语,我也不明白为什么这是一个问题。相反https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html状态:

  

注释类型ComponentScan onfigures组件扫描指令   用于@Configuration类。