2016-05-04 13 views
0

私は春のブートアプリケーションを持っています。 404エラーを処理するカスタムエラーコントローラを設定しました。それは空の応答を返しています。カスタムエラーコントローラメソッドが呼び出されたことを示すログステートメントがあります。しかし、私はSOAP UIまたはChrome Postmanで応答が表示されません。助けてください!!ここで スプリングブートカスタムエラーコントローラ空の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 = falseを

そして、ここに私のログエントリです

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

あなたは@ResponseBody注釈が、ここで必要とされているオブジェクトのカスタムレスポンスJSONを作成することができます
またはclick here

ここで使用されるもののような一般的なエラー応答
関連する問題