لقد قمت مؤخرًا بترقية مشروعنا إلى Spring Boot 2. التطبيق هو مجرد واجهة برمجة تطبيقات Rest. والآن يتم إرجاع جميع ردودنا البالغ عددها 400 و 500 كملف HTML بدلاً من json.
أنا أقوم بتعريف ErrorAttributes المخصصة ، تمامًا كما تقول المستندات.
@Configuration
public class WebConfig implements WebMvcConfigurer {
...
@Bean
public ErrorAttributes errorAttributes() {
return new DefaultErrorAttributes() {
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest,
boolean includeStackTrace) {
Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, true);
return errorAttributes;
}
};
}
...
أرغب في تصحيح هذه المشكلة محليًا ، ولكن لا يمكنني العثور في الرمز حيث يتخذ Spring Boot هذا القرار لإضافة رد JSON للأخطاء. المستندات هنا: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-error-handling تقول:
For machine clients, it produces a JSON response with details of the error, the HTTP status, and the exception message.
أفكر أنه يجب أن يكون لديّ فول محدد محليًا يتسبب في عدم تكوين هذا بشكل صحيح في تكوين Spring Boot Auto.