如何处理Spring数据Rest和PagingAndSortingRepository的异常?

时间:2021-07-16 05:46:02

Let's say I have a repository like:

假设我有一个存储库:

public interface MyRepository extends PagingAndSortingRepository<MyEntity, String> {

    @Query("....")
    Page<MyEntity> findByCustomField(@Param("customField") String customField, Pageable pageable);
}

This works great. However, if the client sends a formed request (say, searching on a field that does not exist), then Spring returns the exception as JSON. Revealing the @Query, etc.

这是伟大的。但是,如果客户端发送一个已形成的请求(例如,在不存在的字段上搜索),那么Spring将以JSON的形式返回异常。揭示了@Query等等。

// This is OK
http://example.com/data-rest/search/findByCustomField?customField=ABC

// This is also OK because "secondField" is a valid column and is mapped via the Query
http://example.com/data-rest/search/findByCustomField?customField=ABC&sort=secondField

// This throws an exception and sends the exception to the client
http://example.com/data-rest/search/findByCustomField?customField=ABC&sort=blahblah

An example of the exception thrown and sent to client:

抛出并发送给客户端的异常示例:

{
    message:null,
    cause: {
        message: 'org.hibernate.QueryException: could not resolve property: blahblah...'
    }
}

How can I handle those exceptions? Normally, I use the @ExceptionHandler for my MVC controllers but I'm not using a layer between the Data Rest API and the client. Should I?

我如何处理这些异常?通常,我为MVC控制器使用@ExceptionHandler,但是我并没有在Data Rest API和客户端之间使用一个层。我应该吗?

Thanks.

谢谢。

2 个解决方案

#1


4  

You could use a global @ExceptionHandler with the @ControllerAdvice annotation. Basically, you define which Exception to handle with @ExceptionHandler within the class with @ControllerAdvice annotation, and then you implement what you want to do when that exception is thrown.

您可以使用带有@ControllerAdvice注释的全局@ExceptionHandler。基本上,您使用@ControllerAdvice注释在类中使用@ExceptionHandler定义要处理的异常,然后在抛出异常时实现您想要执行的操作。

Like this:

是这样的:

@ControllerAdvice(basePackageClasses = RepositoryRestExceptionHandler.class)
public class GlobalExceptionHandler {

    @ExceptionHandler({QueryException.class})
    public ResponseEntity<Map<String, String>> yourExceptionHandler(QueryException e) {
        Map<String, String> response = new HashMap<String, String>();
        response.put("message", "Bad Request");
        return new ResponseEntity<Map<String, String>>(response, HttpStatus.BAD_REQUEST); //Bad Request example
    }
}

See also: http://www.ekiras.com/2016/02/how-to-do-exception-handling-in-springboot-rest-application.html

参见:http://www.ekiras.com/2016/02/how-to-do-exception-handling-in-springboot-rest-application.html

#2


0  

You could use @ControllerAdvice and render the content your way. Here is tutorial if you need know how to work on ControllerAdvice, just remember to return HttpEntity

您可以使用@ControllerAdvice并按自己的方式呈现内容。如果您需要了解如何处理ControllerAdvice,请记住返回HttpEntity

#1


4  

You could use a global @ExceptionHandler with the @ControllerAdvice annotation. Basically, you define which Exception to handle with @ExceptionHandler within the class with @ControllerAdvice annotation, and then you implement what you want to do when that exception is thrown.

您可以使用带有@ControllerAdvice注释的全局@ExceptionHandler。基本上,您使用@ControllerAdvice注释在类中使用@ExceptionHandler定义要处理的异常,然后在抛出异常时实现您想要执行的操作。

Like this:

是这样的:

@ControllerAdvice(basePackageClasses = RepositoryRestExceptionHandler.class)
public class GlobalExceptionHandler {

    @ExceptionHandler({QueryException.class})
    public ResponseEntity<Map<String, String>> yourExceptionHandler(QueryException e) {
        Map<String, String> response = new HashMap<String, String>();
        response.put("message", "Bad Request");
        return new ResponseEntity<Map<String, String>>(response, HttpStatus.BAD_REQUEST); //Bad Request example
    }
}

See also: http://www.ekiras.com/2016/02/how-to-do-exception-handling-in-springboot-rest-application.html

参见:http://www.ekiras.com/2016/02/how-to-do-exception-handling-in-springboot-rest-application.html

#2


0  

You could use @ControllerAdvice and render the content your way. Here is tutorial if you need know how to work on ControllerAdvice, just remember to return HttpEntity

您可以使用@ControllerAdvice并按自己的方式呈现内容。如果您需要了解如何处理ControllerAdvice,请记住返回HttpEntity