我试图在Spring-boot中自定义白标错误页面

时间:2018-02-15 10:14:30

标签: java maven spring-boot

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

enter image description here

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.

2 个答案:

答案 0 :(得分:0)

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

@Setter(AccessLevel.PROTECTED)

@NoArgsConstructor

@Component

公共类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");
}

}

@Log

@PropertySource(&#34; classpath:application.properties&#34;)

@Configuration

@ComponentScan(&#34;玉米&#34)

@RestController

public class 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);
}

}

答案 1 :(得分:0)

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

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