0

I want to override @RepositoryRestResource autogenerated controller methods using @RepositoryRestController having set the SDR's Base Path to "/api".

Spring Data Rest 3.0 (and earlier) says:

"This controller [as shown in the snippet] will be served from the same API base path defined in RepositoryRestConfiguration.setBasePath that is used by all other RESTful endpoints (e.g. /api)". https://docs.spring.io/spring-data/rest/docs/3.0.1.RELEASE/reference/html/#customizing-sdr.overriding-sdr-response-handlers (chapter 15.4)

This code snippet DOES NOT have a @RequestMapping on the class level, though.

My SDR app is configured with RepositoryRestConfiguration object

config.setBasePath("/api");

and yet @RepositoryRestController doesn't override SDR's autogenerated controller methods.

Please consider the accepted answear to this post: Spring Data Rest controllers: behaviour and usage of @BasePathAwareController, @RepositoryRestController, @Controller and @RestController

Please help me understand this! :)

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>{
}
3
  • What do you want to achieve and what's the problem you're facing? Commented Nov 23, 2017 at 9:07
  • Autogenerated controller methods overriding discussed here. Commented Nov 23, 2017 at 9:12
  • @marc-tarin, yes, the topic you've pointed out is ok, but what if you set the base path for SDR. Things change... My controller doesn't override autogen. methods. Commented Nov 23, 2017 at 9:59

1 Answer 1

1

The key to resolve the above dilemma was configuring the project in a correct fashion. That is, to put @ComponentScan in the class passed to AbstractAnnotationConfigDispatcherServletInitializer::getServletConfigClasses() method (not in AppConf.java passed to getRootConfigClasses()).

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 {
}

Even if I solved the riddle I don't understand why it was an issue. The rather that https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html states:

Annotation Type ComponentScan onfigures component scanning directives for use with @Configuration classes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.