2014-12-25 25 views
6

Chúng tôi đang sử dụng Spring-boot cho một ứng dụng dòng lệnh. Chúng tôi đang sử dụng javax.validation để xác thực các đối số dòng lệnh.Khởi động mùa xuân CommandLineRunner Xử lý ngoại lệ

Bây giờ, nếu chúng tôi có lỗi xác thực, chúng tôi có thể in thông báo lỗi thân thiện như thế nào? Chúng tôi không muốn hiển thị dấu vết Stack.

Có cơ chế ExceptionHandler chúng tôi có thể sử dụng khi chúng tôi đang chạy Spring-boot như CommandLineRunner không?

Cảm ơn Arun

Nguồn

@SpringBootApplication 
    public class Deploy implements CommandLineRunner { 
    private static final Logger LOGGER = LoggerFactory.getLogger(Deploy.class); 

    @Autowired 
    private DeployConfig config; 

    @Autowired 
    private DeployService deployService; 

    /** 
    * mvn clean package spring-boot:repackage 
    * java -jar target/spring-boot-example-1.0.0-SNAPSHOT.jar --spring.profiles.active=qa --version=1.0 
    * 
    * @param strings arguments 
    * @throws Exception 
    */ 

    @Override 
    public void run(String... strings) throws Exception { 
     try { 
      deployService.deploy(config); 
     } catch (Exception ve) { 
      LOGGER.error("Error : {}", ve.getMessage()); 
     } 

     LOGGER.info("Created stack={}", config.getVersion()); 
    } 

    public static void main(String... args) { 
     LOGGER.info("Starting to run..."); 
     SpringApplication.run(Deploy.class, args); 
     LOGGER.info("Completed the run..."); 
    } 
} 

Cấu hình

@Configuration 
@EnableConfigurationProperties 
@ConfigurationProperties 
public class DeployConfig { 

    @NotNull 
    private String hello; 

    @NotNull 
    private String version; 

    private String envKey; 

    public String getHello() { 
     return hello; 
    } 

    public void setHello(String hello) { 
     this.hello = hello; 
    } 

    public String getVersion() { 
     return version; 
    } 

    public void setVersion(String version) { 
     this.version = version; 
    } 

    public String getEnvKey() { 
     return envKey; 
    } 

    public void setEnvKey(String envKey) { 
     this.envKey = envKey; 
    } 

    public String toString() { 
     return ToStringBuilder.reflectionToString(this); 
    } 
} 

sạch Run

mvn clean package spring-boot:repackage 
java -jar target/spring-boot-example-1.0.0-SNAPSHOT.jar --spring.profiles.active=preprod,qa --version=1.0 

Validation Kiểm tra

java -jar target/spring-boot-example-1.0.0-SNAPSHOT.jar --spring.profiles.active=preprod,qa 

Validation Lỗi

2014-12-25 20:51:13,325 ERROR [main] [o.s.b.SpringApplication.run()] - Application startup failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'deploy': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.DeployConfig com.example.Deploy.config; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'deployConfig': Could not bind properties; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors 
Field error in object 'target' on field 'version': rejected value [null]; codes [NotNull.target.version,NotNull.version,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.version,version]; arguments []; default message [version]]; default message [may not be null] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.1.3.RELEASE.jar!/:4.1.3.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202) ~[spring-beans-4.1.3.RELEASE.jar!/:4.1.3.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) ~[spring-beans-4.1.3.RELEASE.jar!/:4.1.3.RELEASE] 

Nguồn Toàn bộ

Nguồn có thể được tìm thấy trong GitHub

Trả lời

0

Không, không có built-in cơ chế xử lý ngoại lệ cho trường hợp ngoại lệ xử lý từ CommandLineRunner s - xem org.springframework.boot.SpringApplication#runCommandLineRunners, nó sẽ được dễ dàng hơn để chỉ cần quấn nó xung quanh truyền thống khối xử lý java ngoại trừ của bạn

+0

Thật không may là các câu lệnh gói trong run() trong khối try try không được trợ giúp. Cảm ơn bạn đã trả lời anyways. –

+0

Bạn có thể vui lòng cho biết cách bạn đang sử dụng javax.validation để xác thực đối số của bạn, có thể xem xét điều đó có thể kích hoạt một số ý tưởng khác. –

+0

Ah, tôi thấy quan điểm của bạn ngay bây giờ. Tôi cảm thấy rằng đây là một hành vi có thể chấp nhận được - từ quan điểm của Spring, nếu các thuộc tính khởi động không xác nhận được thì không khởi động ứng dụng nhưng hiển thị các lỗi xác thực chính xác trong stacktrace. Tuy nhiên, điều này không cụ thể đối với 'CommandLineRunners', nó phải là hành vi tương tự với/không có CommandLineRunners. –

2

tôi đã chạy vào (gần như) cùng một vấn đề, và (nhiều đến ngạc nhiên của tôi) dường như cách sạch nhất để hiển thị lỗi cho người dùng từ chương trình dòng lệnh Khởi động Spring là thực sự System.exit(1) từ CommandlineRunner.run(), sau khi ghi nhật ký bất kỳ thông báo lỗi nào bạn muốn. Tuy nhiên, bối cảnh Spring sẽ tắt hoàn toàn, nhưng nó không kích hoạt các sự kiện thất bại khi khởi động ngữ cảnh, vì vậy bạn không nhận được tất cả các đầu ra log distracting khác.

Bạn có thể phải điều chỉnh cách bạn gọi xác thực để bạn có thể tự mình kiểm tra lỗi xác thực trong số run() và dịch chúng thành nhật ký + System.exit().

Các vấn đề liên quan