自定义入门程序提供http端点的最佳实践是什么

时间:2019-03-24 12:02:55

标签: spring-boot spring-boot-starter

假设我有一个自定义启动器:

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

将其导入到项目后,您将具有短信验证功能。按照惯例它将提供一些bean。入门项目中的以下代码片段按约定(使用@Conditional@Bean)提供了一个SmsVerificationCodeSender实例。

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

}

我的问题是如何按惯例提供http端点?

当前,我使用@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
    }

}

我想知道入门项目中这样做的正确方法吗?

1)编写一个@RestController。
2)设置一个@ComponentScan。

我之所以担心,是因为我没有使用@Service/@Repository注释其他bean,而是使用@Bean@Conditional

以编程方式提供了bean实例。

此外,如果您想自定义给定@RequestMapping的路径,则会变得很困难

这是我要实现的目标:

1)入门者如何通过约定提供http端点?
2)如果用户定义了自己的端点,则应跳过默认的http端点
3)它应该为用户提供一些配置选项,例如更改URI等

如果需要更多详细信息,可以查看此repo

0 个答案:

没有答案