英文:
How to set a boolean at controller level in springboot?
问题
我有一个名为WalletController的控制器类,我还有另一个具有布尔属性的转换器类。我在我的控制器的每个方法中将该布尔值设置为true。是否有一种方法可以在控制器级别上设置该布尔值,而不是在方法级别上,以便该布尔值只对这个特定的控制器为true?
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/api/wallet")
public class WalletController {
private static final Logger logger = LoggerFactory.getLogger(WalletController.class);
@Autowired
private VoucherService voucherService;
@Autowired
MailManager mailManager;
@Autowired
Converter converter;
// ... (其他方法的定义)
@CrossOrigin(origins = "*")
@RequestMapping(value = "/create", method = RequestMethod.POST)
public @ResponseBody ResponseEntity saveTransaction(@RequestBody CreateTopupDTO createTopupDTO, HttpServletRequest httpServletRequest) {
//converter.supports = true;
System.out.println("------------------" + httpServletRequest.getRequestURI());
return voucherService.createVoucher(createTopupDTO);
}
// ... (其他方法的定义)
@CrossOrigin(origins = "*")
@RequestMapping(value = "/withdraw", method = RequestMethod.POST)
public @ResponseBody ResponseEntity createWithdrawRequest(@RequestBody CreateWithdrawDTO withdrawDTO, HttpServletRequest httpServletRequest) {
//converter.supports = true;
return voucherService.createWithdrawRequest(withdrawDTO);
}
// ... (其他方法的定义)
// 其他方法的定义...
}
英文:
I have a controller class named WalletController, I also have another converter class that have attribute boolean. I am setting that boolean to true at every method in my controller. Is there a way to set that boolean at controller level instead of method level, so that boolean would only be true for this particular controller only?
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/api/wallet")
public class WalletController {
private static final Logger logger = LoggerFactory.getLogger(WalletController.class);
@Autowired
private VoucherService voucherService;
@Autowired
MailManager mailManager ;
@Autowired
Converter converter;
@CrossOrigin(origins = "*")
@RequestMapping(value = "/create", method = RequestMethod.POST)
public @ResponseBody ResponseEntity saveTransaction(@RequestBody CreateTopupDTO createTopupDTO, HttpServletRequest httpServletRequest) {
//converter.supports = true;
System.out.println("------------------"+httpServletRequest.getRequestURI());
return voucherService.createVoucher(createTopupDTO);
}
@CrossOrigin(origins = "*")
@RequestMapping(value = "/withdraw", method = RequestMethod.POST)
public @ResponseBody ResponseEntity createWithdrawRequest(@RequestBody CreateWithdrawDTO withdrawDTO, HttpServletRequest httpServletRequest) {
//converter.supports = true;
return voucherService.createWithdrawRequest(withdrawDTO);
}
@CrossOrigin(origins = "*")
@RequestMapping(value = "/currency", method = RequestMethod.GET)
public @ResponseBody ResponseEntity getListOfCurrencies(HttpServletRequest httpServletRequest) {
//converter.supports = true;
return voucherService.getListOfCurrencies();
}
@CrossOrigin(origins = "*")
@RequestMapping(value = "/transactions", method = RequestMethod.POST)
public @ResponseBody ResponseEntity getTransactions(@RequestBody TransactionInfoDTO transactionInfoDTO, HttpServletRequest httpServletRequest) {
//converter.supports = true;
return voucherService.getTransactions(transactionInfoDTO);
}
@CrossOrigin(origins = "*")
@RequestMapping(value = "/transactions/properties", method = RequestMethod.GET)
public @ResponseBody ResponseEntity getPropertiesTransactions( HttpServletRequest httpServletRequest) {
//converter.supports = true;
return voucherService.getPropertiesTransactions();
}
@CrossOrigin(origins = "*")
@RequestMapping(value = "/balance", method = RequestMethod.GET)
public @ResponseBody ResponseEntity getBalance(HttpServletRequest httpServletRequest) {
//converter.supports = true;
return voucherService.getBalance();
}
@CrossOrigin(origins = "*")
@RequestMapping(value = "/complete/balance", method = RequestMethod.GET)
public @ResponseBody ResponseEntity getWalletBalance(HttpServletRequest httpServletRequest) {
//converter.supports = true;
return voucherService.getWalletBalance();
}
@CrossOrigin(origins = "*")
@RequestMapping(value = "/complete/balance/v2", method = RequestMethod.GET)
public @ResponseBody ResponseEntity getWalletBalanceV2(HttpServletRequest httpServletRequest) {
//converter.supports=true;
return voucherService.getWalletBalanceV2();
}
@CrossOrigin(origins = "*")
@RequestMapping(value = "/banks", method = RequestMethod.GET)
public @ResponseBody ResponseEntity getBanks(HttpServletRequest httpServletRequest) {
//converter.supports=true;
return voucherService.getDepositBanks();
}
@CrossOrigin(origins = "*")
@RequestMapping(value = "/properties", method = RequestMethod.GET)
public @ResponseBody ResponseEntity getProperties( HttpServletRequest httpServletRequest) {
//converter.supports=true;
return voucherService.getPropertyDetailsForTransactions();
}
@CrossOrigin(origins = "*")
@RequestMapping(value = "/valuationfee", method = RequestMethod.POST)
public @ResponseBody ResponseEntity saveValuationFee(@RequestBody ValuationFeeDTO dto, HttpServletRequest httpServletRequest) {
//converter.supports=true;
return voucherService.saveValuationFee(dto);
}
}
答案1
得分: 0
你可以为转换器类创建自定义的Bean(避免在类上使用@Component注解),并在需要的时候在控制器中进行注入。
例如:
假设你有一个这样的Converter
类:
public class Converter {
private boolean supports = false;
public boolean isSupports() {
return supports;
}
public void setSupports(boolean supports) {
this.supports = supports;
}
}
在一个@Configuration
类中创建自定义的Converter Bean。
@Configuration
public class SampleConfig {
@Bean(name = "converterSupportTrue")
public Converter beanOne() {
Converter converter = new Converter();
converter.setSupports(true);
return converter;
}
@Bean(name = "converterSupportFalse")
public Converter beanTwo() {
Converter converter = new Converter();
return converter;
}
}
用于测试的控制器:
@Controller
public class FirstController {
@Autowired
private Converter converterSupportTrue;
@GetMapping("/test/support1")
public void test() {
System.out.println(converterSupportTrue.isSupports());
}
}
另一个用于测试的控制器:
@Controller
public class SecondController {
@Autowired
private Converter converterSupportFalse;
@GetMapping("/test/support2")
public void test() {
System.out.println(converterSupportFalse.isSupports());
}
}
你可以在整个应用程序中使用这个Bean,而无需编写样板代码。
英文:
You can create your custom bean for converter class (avoid @Component over the class) and inject it in controller when required.
For example:
Suppose you have a Converter
class like this :
public class Converter {
private boolean supports = false;
public boolean isSupports() {
return supports;
}
public void setSupports(boolean supports) {
this.supports = supports;
}
}
Create custom Converter bean in a @Configuration
class.
@Configuration
public class SampleConfig {
@Bean(name = "converterSupportTrue")
public Converter beanOne() {
Converter converter = new Converter();
converter.setSupports(true);
return converter;
}
@Bean(name = "converterSupportFalse")
public Converter beanTwo() {
Converter converter = new Converter();
return converter;
}
}
Controller to test :
@Controller
public class FirstController {
@Autowired
private Converter converterSupportTrue;
@GetMapping("/test/support1")
public void test() {
System.out.println(converterSupportTrue.isSupports());
}
}
Another controller to test ;
@Controller
public class SecondController {
@Autowired
private Converter converterSupportFalse;
@GetMapping("/test/support2")
public void test() {
System.out.println(converterSupportFalse.isSupports());
}
}
You can use this bean throughout the application without writing boilerplate code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论