2016-04-20 7 views
7

スプリングブートレストサービスがあります。パスが間違っているときは何も返しません。全く応答なし。それと同時に、エラーもスローされません。理想的には404エラーが見つかりませんでした。スプリングブートレスト - 404リソースの設定方法

私はこの方法では、私は私の性質でerror.whitelabel.enabled=false

私はスローするように、このサービスのために他に何をしなければなりませんがマークしているResponseEntityExceptionHandler

protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, 
                HttpStatus status, WebRequest request) { 

    return handleExceptionInternal(ex, null, headers, status, request); 
} 

でありGlobalErrorHandler

@ControllerAdvice 
public class GlobalErrorHandler extends ResponseEntityExceptionHandler { 

} 

を得ました404が見つかりませんでしたクライアントへの返信

私は多くのスレッドを参照し、誰にも直面しているこの問題は見ません。

これは

@EnableAutoConfiguration // Sprint Boot Auto Configuration 
@ComponentScan(basePackages = "com.xxxx") 
@EnableJpaRepositories("com.xxxxxxxx") // 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(false).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; 
    } 
} 

答えて

13

私のメインアプリケーションクラスであるソリューションは非常に簡単です:

まずあなたはすべてのエラーケースを処理するコントローラを実装する必要があります。このコントローラーには@ControllerAdviceが必要です - すべて@RequestMappingsに適用される@ExceptionHandlerを定義する必要があります。

@ControllerAdvice 
public class ExceptionHandlerController { 

    @ExceptionHandler(NoHandlerFoundException.class) 
    @ResponseStatus(value= HttpStatus.NOT_FOUND) 
    @ResponseBody 
    public ErrorResponse requestHandlingNoHandlerFound() { 
     return new ErrorResponse("custom_404", "message for 404 error code"); 
    } 
} 

応答を無効にする場合は、@ExceptionHandlerに指定してください。 NoHandlerFoundExceptionは、Springが要求を委譲できない場合に生成される例外です(404件)。 Throwableを指定して、例外を無効にすることもできます。あなたは(ハンドラを解決できませんでした)404の場合に例外をスローするように春を伝える必要があり

第二

@SpringBootApplication 
@EnableWebMvc 
public class Application { 

    public static void main(String[] args) { 
     ApplicationContext ctx = SpringApplication.run(Application.class, args); 

     DispatcherServlet dispatcherServlet = (DispatcherServlet)ctx.getBean("dispatcherServlet"); 
     dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); 
    } 
} 

の検索結果を私は非定義されたURL

{ 
    "errorCode": "custom_404", 
    "errorMessage": "message for 404 error code" 
} 
+0

を使用するときは、ありがとうございます。この変更を行った後も、私はまだ応答が見られません。助けてください!!! – Shiv

+0

フィルタ実装を投稿できますか? CorrelationHeaderFilterはすべてのリクエストを処理し、それがあなたにとってうまくいかない理由かもしれません。私はあなたのコードに非常に近いコードでテストし、フィルタにコメントを付け、 '@EnableWebMvc'を追加し、 '@EnableSwagger'、 '@@ EnableJpaRepositories'、 '@@ EnableAspectJAutoProxy'のような注釈は不要です。 –

+0

私は単純なアプリケーションをgithubに投稿できます。 –

関連する問題