2016-05-04 126 views
0

我有一個春季啓動應用程序。我配置了一個自定義錯誤控制器來處理404錯誤。它正在返回一個空的答覆。我得到了表明自定義錯誤控制器方法被調用的日誌語句。但是我在SOAP UI或Chrome Postman中看不到回覆。請幫忙!!Spring Boot自定義錯誤控制器返回空JSON響應

@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); 
    } 

} 

這裏是我的應用程序類

@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; 
    } 
} 

而且我有這個在我的屬性文件

錯誤配置

error.whitelabel.enabled =假

這裏是我的日誌條目

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 

回答

1

您可以使用控制器建議和單獨的休息錯誤處理程序並處理每個異常類型。
see this answer

您可以創建自定義的響應JSON對象這裏所需要的註解@ResponseBody
或喜歡這裏使用click here

了一個通用的錯誤響應
相關問題