问题描述
假设我有一个存储库,如: public interface MyRepository扩展了PagingAndSortingRepository&MyEntity,String> {
@Query(....)
页面< MyEntity> findByCustomField(@Param(customField)String customField,pageable pageable);
}
这个功能很好。但是,如果客户端发送一个形成的请求(比如说在不存在的字段上进行搜索),那么Spring将返回异常作为JSON。显示 @Query
等。
//这是OK b $ b http://example.com/data-rest/search/findByCustomField?customField=ABC
//这也是可以的,因为secondField是一个有效的列,并通过Query
http://example.com/data-rest/search/findByCustomField?customField=ABC&sort=secondField
//这会抛出异常并将异常发送到客户端
http://example.com/data-rest/search/findByCustomField?customField=ABC&sort=blahblah
抛出并发送给客户端的例外情况:
{
/ pre>
message:null,
原因:{
message:'org.hibernate.QueryException:无法解析属性:blahblah ...'
}
}
如何处理这些异常?通常,我使用我的MVC控制器的
@ExceptionHandler
,但是我没有在Data Rest API和客户端之间使用一个层。我应该吗?
谢谢。
解决方案与注释。基本上,您使用@ControllerAdvice注释定义要在类中使用@ExceptionHandler处理哪个异常,然后在实例发生异常时实现您想要执行的操作。
这个:
@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); //错误请求示例
}
}
另请参见:
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.// 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?Thanks.
解决方案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.
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
这篇关于如何使用Spring Data Rest和PagingAndSortingRepository来处理异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!