1

Let's say I have a custom starter:

compile ('com.github.hippoom:sms-verification-starter:0.7.0')

You'll have a sms verification feature once you import it to your project. It will provide some beans by convention. The following code snippet in the starter project provides a SmsVerificationCodeSender instance by convention (using @Conditional and @Bean ).

// the starter project includes the following

public interface SmsVerificationCodeSender {
}

@Configuration
public class ApplicationConfiguration {

    @Bean
    @ConditionalOnMissingBean(SmsVerificationCodeSender.class)
    public SmsVerificationCodeSender smsVerificationCodeSenderStub() {
        // setup and return smsVerificationCodeSenderStub instance
    }

}

My question is how do I provide http endpoints by convention?

Currently, I make it by using @ComponentScan

// the starter project includes the following as well
//TODO figure out is @ComponentScan is a good practice or not?
@ComponentScan(basePackages = {
        "com.thebund1st.daming.web",
})
@Import({
        OtherConfiguration.class
})
@Configuration
public class MyAutoConfiguration {


}

package com.thebund1st.daming.web

@RequiredArgsConstructor
@RestController
//TODO make the url path configurable
public class SmsVerificationRestController {

    @PostMapping("/api/sms/verification/code")
    @ResponseStatus(ACCEPTED)
    public void handle(@RequestBody SendSmsVerificationCodeCommand command) {
    //omitted codes
    }

}

I'm wondering is it the correct way to do this in the starter project?

1) Write a @RestController .
2) Setup a @ComponentScan .

I have this concern just because I don't annotate other beans with @Service/@Repository, instead I provide the bean instances programmatically by using @Bean and @Conditional

Besides, it makes it difficult if you want to customize the path of a given @RequestMapping

Here is what I want to achieve:

1) How do a starter provides http endpoints by convention?
2) It should skip the default http endpoints if the user defines his/her own endpoints
3) It should provide some configuration options for the user, like changing URIs etc

You can have a look at this repo if more details are needed.

Yugang Zhou
  • 7,123
  • 6
  • 32
  • 60
  • Spring Boot follows a `convention over configuration` approach, therefore defining `ComponentScan` yourself isn't really good practice imo. – gkhaos Mar 24 '19 at 12:11
  • Yes, I don't want to use @ComponentScan either, just have no idea of how to do it in a better way – Yugang Zhou Mar 24 '19 at 12:39
  • 1
    I did not yet figured out what you want to achieve. I'd appreciate if you could give me/us an example of a use-case including the components you are using for this use-case as well as what you try to test. – gkhaos Mar 24 '19 at 14:20
  • @moneydhaze thank you for the patience, I have updated the question. Please leave a note if there is anything need to be explained further, thank you – Yugang Zhou Mar 25 '19 at 02:49

0 Answers0