[英]Spring Boot custom error controller returning empty JSON response
I got a Spring Boot app. 我有一个Spring Boot应用程序。 I have configured a custom error controller to handle 404 errors.
我已经配置了一个自定义错误控制器来处理404错误。 It is returning an empty response.
它返回一个空响应。 I got log statements which shows that the custom error controller method is called.
我得到的日志语句表明已调用自定义错误控制器方法。 But I don't see a response in my SOAP UI or Chrome Postman.
但是我在SOAP UI或Chrome Postman中没有看到响应。
@RestController
public class MyErrorController implements ErrorController {
private static final String PATH = "/error";
private boolean debug = true;
private static final Logger log = LoggerFactory.getLogger(MyErrorController.class);
@Autowired
private ErrorAttributes errorAttributes;
@RequestMapping(value = PATH)
public MyGlobalError error(HttpServletRequest request, HttpServletResponse response) {
log.error("***" + RequestCorrelation.getId() + "***" + "MyErrorController - error ()");
MyGlobalError error = new MyGlobalError(request.getMethod(), response.getStatus(),
getErrorAttributes(request, debug));
return error;
}
@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);
}
}
Here is my app class 这是我的应用程序类
@EnableAutoConfiguration // Sprint Boot Auto Configuration
@ComponentScan(basePackages = "com.app")
@EnableJpaRepositories("com.app.jpa") // To segregate MongoDB
// and JPA repositories.
// Otherwise not needed.
@EnableSwagger // auto generation of API docs
@SpringBootApplication
@EnableAspectJAutoProxy
@EnableConfigurationProperties
public class Application extends SpringBootServletInitializer {
private static Class<Application> appClass = Application.class;
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(appClass).properties(getProperties());
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public FilterRegistrationBean correlationHeaderFilter() {
FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
filterRegBean.setFilter(new CorrelationHeaderFilter());
filterRegBean.setUrlPatterns(Arrays.asList("/*"));
return filterRegBean;
}
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
static Properties getProperties() {
Properties props = new Properties();
props.put("spring.config.location", "classpath:/");
return props;
}
@Bean
public WebMvcConfigurerAdapter webMvcConfigurerAdapter() {
WebMvcConfigurerAdapter webMvcConfigurerAdapter = new WebMvcConfigurerAdapter() {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false).favorParameter(true).parameterName("media-type")
.ignoreAcceptHeader(true).useJaf(false).defaultContentType(MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML).mediaType("json", MediaType.APPLICATION_JSON);
}
};
return webMvcConfigurerAdapter;
}
@Bean
public RequestMappingHandlerMapping defaultAnnotationHandlerMapping() {
RequestMappingHandlerMapping bean = new RequestMappingHandlerMapping();
bean.setUseSuffixPatternMatch(false);
return bean;
}
}
And I have this in my properties file 我的属性文件中有这个
#error configurations
error.whitelabel.enabled=false
And here is my log entry 这是我的日志条目
2016-05-04 12:26:45 - ***f37dbfea-daeb-4d35-bac3-73bb5fb25c1c***-Entering: MyErrorController.error
2016-05-04 12:26:45 - ***f37dbfea-daeb-4d35-bac3-73bb5fb25c1c***MyErrorController - error ()
2016-05-04 12:26:45 - ***f37dbfea-daeb-4d35-bac3-73bb5fb25c1c***-Exited: MyErrorController.error
you can use a controller advice and a separate rest error handler and handle each exception type. 您可以使用控制器建议和单独的rest错误处理程序,并处理每种异常类型。
see this answer 看到这个答案
you can create a custom response json object the @ResponseBody annotation is needed here 您可以创建一个自定义响应json对象,这里需要@ResponseBody批注
or a generic error response like the one used here click here 或类似此处使用的一般错误响应, 请单击此处
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.