【问题标题】:I am trying to customize White Label Error page in Spring-boot我正在尝试在 Spring-boot 中自定义白标错误页面
【发布时间】:2018-07-26 01:23:35
【问题描述】:

我正在使用 Spring Boot 1.5.10 我收到错误“启动 ApplicationContext 时出错。要显示自动配置报告,请在启用 'debug' 的情况下重新运行您的应用程序。我附上了错误图片

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-02-15 15:31:45.872 ERROR 6968 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.serviceimpl.ErrorJson required a bean of type 'int' that could not be found.


Action:

Consider defining a bean of type 'int' in your configuration.

【问题讨论】:

  • 提供一些代码sn-p。您是如何尝试实现这一目标的。
  • @JaydeepPatel 请检查我提供的答案

标签: java maven spring-boot


【解决方案1】:

@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY) @Getter(AccessLevel.PUBLIC)

@Setter(AccessLevel.PROTECTED)

@NoArgsConstructor

@组件

公共类 ErrorJson {

public Integer status;
public String error;

@JsonIgnore
public String message;
public String timeStamp;
public String trace;


public ErrorJson(int status, Map<String, Object> errorAttributes) {
    this.status = status;
    this.error = (String) errorAttributes.get("error");
    this.message = (String) errorAttributes.get("message");
    this.timeStamp = errorAttributes.get("timestamp").toString();
    this.trace = (String) errorAttributes.get("trace");
}

}


@日志

@PropertySource("classpath:application.properties")

@配置

@ComponentScan("com")

@RestController

公共类 CustomErrorController 实现 ErrorController {

private static final String PATH = "/error";

@Value("${custom-error-controller.debug}")
private boolean debug;

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
    return new PropertySourcesPlaceholderConfigurer();
}

@Autowired
private ErrorAttributes errorAttributes;

@RequestMapping(value = PATH)
ResponseEntity<ErrorJson> error(HttpServletRequest request, HttpServletResponse response){
     return ResponseEntity.status(response.getStatus())
         .body(
             new ErrorJson(response.getStatus(), getErrorAttributes(request, debug)
         )
     );
}

@Override
public String getErrorPath(){
    return PATH;
}

private Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    return errorAttributes.getErrorAttributes(requestAttributes, includeStackTrace);
}

}

【讨论】:

  • 我创建了 No args 构造函数并注释掉了@noArgConstructor 问题解决了
【解决方案2】:

您需要从 ErrorJson 类中删除 @Component 注释。此注解告诉 Spring 将类创建为 bean,它不需要这样做。

编辑:请注意,创建无参数构造函数只是掩盖了问题,因为它只是允许 Spring 创建类而无需填写您的参数,而 Spring 根本不应该参与这个类。

【讨论】:

    猜你喜欢
    • 2020-12-04
    • 2018-02-11
    • 2016-08-04
    • 2017-08-07
    • 2014-11-30
    • 2017-10-03
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    相关资源
    最近更新 更多