Spring Jpa Repository引发的异常未在异常处理程序@Controller通知中捕获

时间:2018-12-15 10:29:12

标签: java spring spring-boot spring-restcontroller spring-rest

下面是我在Spring Boot中的ExceptionHandler类

import { Component, OnInit, ViewChild } from '@angular/core';
import {MatPaginator, MatTableDataSource} from '@angular/material';
import { MatTableDataSource, MatSort } from '@angular/material';
    import { DataSource } from '@angular/cdk/table';

@Component({
  selector: 'app-testm',
  templateUrl: './testm.component.html',
  styleUrls: ['./testm.component.scss']
})
export class TestmComponent implements OnInit {

  constructor() { }
  dataSource;
  displayedColumns = [];
  @ViewChild(MatSort) sort: MatSort;
  @ViewChild(MatPaginator) paginator: MatPaginator;


  /**
   * Pre-defined columns list for user table
   */
  columnNames = [{
    id: "position",
    value: "No."

  }, {
    id: "name",
    value: "Name"
  },
  {
    id: "weight",
    value: "Weight"
  },
  {
    id: "symbol",
    value: "Symbol"
  }];

  ngOnInit() {
    this.displayedColumns = this.columnNames.map(x => x.id);
        this.createTable();
    this.dataSource.paginator = this.paginator;
  }
  createTable() {
    let tableArr: Element[] = [
    { position: 1, name: 'Hydrogen', weight: 12, symbol: 'H' },
    { position: 2, name: 'Helium', weight: 32, symbol: 'He' },
    { position: 3, name: 'Lithium', weight: 35, symbol: 'Li' },
    { position: 4, name: 'Beryllium', weight: 65, symbol: 'Be' },
    { position: 5, name: 'Boron', weight: 98, symbol: 'B' },
    { position: 6, name: 'Carbon', weight: 49, symbol: 'C' }
    ];
    this.dataSource = new MatTableDataSource(tableArr);
    this.dataSource.sort = this.sort;
  }

}
export interface Element {
  position: number,
  name: string,
  weight: number,
  symbol: string
}

}

下面是我的服务类中的代码。

@RestControllerAdvice
public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {


@ExceptionHandler({InvalidRequestException.class, PropertyReferenceException.class})
public final ResponseEntity<ExceptionDetails> handleInvalidRequestException(InvalidRequestException ex, WebRequest request) {
    ExceptionDetails details = new ExceptionDetails(ex.getMessage(),request.getDescription(false), new Date());
    return new ResponseEntity<ExceptionDetails>(details, HttpStatus.BAD_REQUEST);

}

@Override public Page<Segment> listSegment(int pageNum, int size, String sortBy, String direction, String app) { log.info("Starting to query the segment list"); Pageable pageable = createPageRequest(size, pageNum, sortBy, direction); Pageable pageable = new PageRequest(page, size, dir, sortBy); Page<Segment> resultPage = segmentRepository.findByApp(app,pageable); if(pageNum > resultPage.getTotalPages() || resultPage.getContent().isEmpty()) { log.info("No segment present"); throw new ResourceNotFoundException("No Segment Present"); } log.info("Total segment successfully fetched is: "+resultPage.getContent().size()); if(resultPage.hasNext()) return new Page<>(resultPage.getContent(), true); else return new Page<>(resultPage.getContent()); } 中,如果我传递了无效的列名,则抛出sortBy,但上面的代码没有捕获到我的ExceptionHandler中,我也不知道为什么。

但是,如果我在Service层中使用try-catch块捕获了此异常,并抛出了自定义PropertyReferenceException,则该异常将被捕获在异常处理程序中。

能否请某人让我知道原因以及如何在ExceptionHandler中捕获InvalidRequestException

1 个答案:

答案 0 :(得分:0)

问题是您的这一行-@ExceptionHandler({InvalidRequestException.class, PropertyReferenceException.class})与方法签名handleInvalidRequestException(InvalidRequestException ex, WebRequest request)之间不匹配。

您正在注释该方法以处理两个异常,但是在方法参数中仅指定一个&该异常是一个非常特定的异常,因此它被称为InvalidRequestException。如果您希望两个异常都与同一方法匹配,则方法参数必须是这两个异常的父类。

您需要更改slimane回答的方法,或者有两种处理程序方法-每个异常一个。

@ExceptionHandler({InvalidRequestException.class}) handleInvalidRequestException(InvalidRequestException ex, WebRequest request)

@ExceptionHandler({PropertyReferenceException.class}) handlePropertyReferenceException(PropertyReferenceException ex, WebRequest request)