在SpringBoot Web-flux中管理NullPointer异常

时间:2019-10-12 17:01:09

标签: java spring-boot error-handling spring-webflux

我的代码从元素的Flux基(已创建)中调用Mono元素。包括具有“ getJobById”的@Repository类和用于处理诸如getAll,getJobById,发布,放置,删除之类的请求的@Component类“ Handler”。如果我输入了错误的ID-无法处理异常并收到NullPointerException错误。 我想捕获异常。

错误:

java.lang.NullPointerException: null at com.javasampleapproach.webflux.repo.impl.JobClientRepositoryImpl.getJobById(JobClientRepositoryImpl.java:32) ~[classes/:na]
    at com.javasampleapproach.webflux.functional.handler.JobClientHandler.getJobById(JobClientHandler.java:52) ~[classes/:na]

已经尝试过:

@ControllerAdvice
public class ClientExceptionController  {
@ExceptionHandler(value = ClientNotfoundException.class)
public ResponseEntity<Object> exception(ClientNotfoundException exception) {return new ResponseEntity<>("Id not found", HttpStatus.NOT_FOUND);}

+

public class ClientNotfoundException extends NullPointerException{
    public ClientNotfoundException() {}
    public ClientNotfoundException(String s) {
        super(s);
    }

and in Handler and Repository classes:

if(jobClientRepository.getJobById(jobId).equals(null)) throw new ClientNotfoundException();

我也尝试过:

 Mono<JobClient> jobMono = jobClientRepository.getJobById(jobId)
.onErrorResume(e->Mono.empty());
 @Component
    public Mono<ServerResponse> getJobById(ServerRequest request) {
            long jobId = Long.valueOf(request.pathVariable("id"));
            Mono<ServerResponse> notFound = ServerResponse.notFound().build();

我尝试过:

  if(jobClientRepository.getJobById(jobId).equals(null)) throw new ClientNotfoundException();

错误出现在这里:

Mono<JobClient> jobMono = jobClientRepository.getJobById(jobId).onErrorResume(e->Mono.empty());
return jobMono.flatMap(job -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(fromObject(job))).switchIfEmpty(notFound).onErrorResume(ClientNotfoundException.class, e -> notFound);

@Repository 
@Override
public Mono<JobClient> getJobById(Long id)  {if(jobStores.get(id).equals(null)) throw new ClientNotfoundException();
return Mono.just(jobStores.get(id)).onErrorResume(e -> Mono.error(new ClientNotfoundException("Correct ID is required"+e)));

2 个答案:

答案 0 :(得分:0)

尝试一下:

@Repository 
@Override
public Mono<JobClient> getJobById(Long id)  {
    if(jobStores.get(id) == null) throw new ClientNotfoundException();
    return Mono.just(jobStores.get(id)).onErrorResume(e -> Mono.error(new ClientNotfoundException("Correct ID is required"+e)));
}

在Java中,您首先通过检查给定对象是否为null来避免NullPointerException,然后继续调用其方法,如

if (obj != null) {
    obj.doSomething();
}

答案 1 :(得分:0)

您需要将常规编程与反应性编程分开。试试这个。

@Repository 
@Override
public Mono<JobClient> getJobById(Long id)  {
    return Optional.ofNullable(jobStore.get(id))
            .map(Mono::just)
            .orElseGet(Mono::empty);
}

//Then call it
getJobById(1L).map(jobClient -> ServerResponse.ok()
            .contentType(MediaType.APPLICATION_JSON)
            .syncBody(jobClient)
        ).switchIfEmpty(Mono.error(ClientNotfoundException::new));

运行在手机上编写的代码。