如何将Spring-Mobile项目中的这段Java代码改写为Switch Case形式?
Refactoring Your Spring Mobile Device Redirect Logic to Switch-Case
Got it, let's clean up your code into a more maintainable switch-case structure while fixing a small bug I spotted in your original tablet error handling. Here's how to do it right for your Spring Mobile project:
First, let's address the issue in your original code: your tablet catch block was incorrectly setting the success redirect URL on failure—we'll fix that to use the proper failure URL instead.
Option 1: Using String-Based Switch (Quick and Simple)
This approach is straightforward if you don't want to add extra enum definitions:
// First, categorize the device type String deviceType; if (device.isMobile()) { deviceType = "MOBILE"; } else if (device.isTablet()) { deviceType = "TABLET"; } else if (device.isNormal()) { deviceType = "NORMAL"; } else { // Fallback to normal web device if type is unknown deviceType = "NORMAL"; } // Handle success and failure with consolidated try-catch try { switch (deviceType) { case "MOBILE": redirectUri = cfg.getEmail().getVerification().getMobile_success_redirect_url(); break; case "TABLET": redirectUri = cfg.getEmail().getVerification().getTablet_success_redirect_url(); break; case "NORMAL": default: redirectUri = cfg.getEmail().getVerification().getWeb_success_redirect_url(); break; } } catch (Exception ex) { // Set failure URL based on device type switch (deviceType) { case "MOBILE": redirectUri = cfg.getEmail().getVerification().getMobile_failure_redirect_url(); break; case "TABLET": redirectUri = cfg.getEmail().getVerification().getTablet_failure_redirect_url(); // Fixed your original typo here break; case "NORMAL": default: redirectUri = cfg.getEmail().getVerification().getWeb_failure_redirect_url(); break; } logger.error("Failed to verify email. Cause: {}", ex); } response.sendRedirect(redirectUri);
Option 2: Using Enum (Type-Safe and Clean)
For better type safety and to avoid string typos, define an enum for device types:
// Define an enum (can be a top-level class or nested inside your component) private enum DeviceType { MOBILE, TABLET, NORMAL } // Determine the device type using the enum DeviceType deviceType; if (device.isMobile()) { deviceType = DeviceType.MOBILE; } else if (device.isTablet()) { deviceType = DeviceType.TABLET; } else { deviceType = DeviceType.NORMAL; } // Switch with enum for type-safe logic try { switch (deviceType) { case MOBILE: redirectUri = cfg.getEmail().getVerification().getMobile_success_redirect_url(); break; case TABLET: redirectUri = cfg.getEmail().getVerification().getTablet_success_redirect_url(); break; case NORMAL: redirectUri = cfg.getEmail().getVerification().getWeb_success_redirect_url(); break; } } catch (Exception ex) { switch (deviceType) { case MOBILE: redirectUri = cfg.getEmail().getVerification().getMobile_failure_redirect_url(); break; case TABLET: redirectUri = cfg.getEmail().getVerification().getTablet_failure_redirect_url(); break; case NORMAL: redirectUri = cfg.getEmail().getVerification().getWeb_failure_redirect_url(); break; } logger.error("Failed to verify email. Cause: {}", ex); } response.sendRedirect(redirectUri);
Key Improvements:
- Consolidated Try-Catch: Instead of wrapping each device check in its own try-catch, we use a single block since all the
cfg.getEmail()calls can throw the same exceptions—this reduces redundant code. - Fixed Bug: Corrected the tablet failure redirect URL (your original code used the success URL here, which was likely a mistake).
- Cleaner Logic: Switch-case makes the device-specific logic easier to read and maintain compared to multiple separate if-try-catch blocks.
内容的提问来源于stack exchange,提问作者Sharmistha Chakraborty
相关产品推荐
相关产品推荐

