首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当@requestparam丢失时自定义错误响应消息

当@requestparam丢失时自定义错误响应消息
EN

Stack Overflow用户
提问于 2014-04-23 12:36:09
回答 1查看 11.8K关注 0票数 3

我已经查看了堆栈溢出上的一堆现有页面,但这些都没有帮助:

How to customize @RequestParam error 400 response in Spring MVC

How to inform callers when required rquest parameter is missing?

我的问题非常相似:

  • 我的应用程序实际上是一个REST,返回json/xml数据而不是html (即我不使用jsps,而是编组将java转换为json/xml)。
  • 当我用一个必需的参数查询URL时,我得到了一个400状态,在有效负载中没有任何内容,没有文本,nada。
  • 我想使用我在其他地方使用的ErrorResponse对象返回一个json/xml有效负载。
  • 我正在使用Spring3.2.5
  • 我有一个控制器,它将URL映射到具有所需参数的方法(CandidateBillDay) @Controller公共类AccountController扩展了WsController {私有静态最终字符串JSP_CANDIDATE_PDD = "candidatepdd";@RequestMapping( value="/account/{externalId}/{externalIdType}/candidatepdd“,method = RequestMethod.GET)公共字符串getCandidatePaymentDueDateInfo( ModelMap模型,@PathVariable String externalId,@PathVariable Integer externalIdType,@RequestParam整数candidateBillDay,@RequestParam(required=false) Boolean ){ .model.addAttribute( CandidatePaymentDueDateResponse.ROOT_ELEMENT,.);返回JSP_CANDIDATE_PDD;}
  • 我有一个异常处理程序,它捕获所有类型的异常,具有为某些类型(Instanceof)执行一些特定位的逻辑: @ControllerAdvice公共类BWSExceptionHandler扩展了ResponseEntityExceptionHandler {私有静态最终日志=ResponseEntityExceptionHandler @ExceptionHandler(value ={ Exception.class }) public ResponseEntity handleOtherExceptions(最终异常ex,最终WebRequest req) { LOG.error("Uncaught:",ex);ErrorResponse resp = null;若( MissingServletRequestParameterException的实例){ MissingServletRequestParameterException e= (MissingServletRequestParameterException) ex;resp =新ErrorResponse( Validatable.ERR_CODE_FIELD_NOT_POPULATED,String.format( Validatable.MSG_FIELD_IS_REQUIRED,e.getParameterName( );httpStatusCode = HttpStatus.BAD_REQUEST;} if(resp==null){ resp =新ErrorResponse(新ErrorElement("unknown_error",ex.getMessage();}返回handleExceptionInternal(ex,resp,新HttpHeaders(),httpStatusCode,req);}

因此,当缺少参数时,它不会执行任何操作。当我得到一个实际的异常(即帐户不存在)时,它就会捕获异常并作为例外进行工作。这使我认为没有抛出MissingServletRequestParameterException异常,根据文档、博客和堆栈溢出页面,应该抛出.

我还尝试实现了一个类,它扩展了DefaultHandlerExceptionResolver并覆盖了handleMissingServletRequestParameter方法,但没有取得多大的成功(跟随这个博客:http://alexcuesta.wordpress.com/2011/05/11/error-handling-and-http-status-codes-with-spring-mvc/ )

你知不知道我做错了什么,或者我应该探索什么其他选择?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-23 16:34:33

尝试在handleMissingServletRequestParameter类中重写BWSExceptionHandler方法。

代码语言:javascript
运行
复制
@ControllerAdvice
public class BWSExceptionHandler extends ResponseEntityExceptionHandler {
  ...
  @Override
  protected ResponseEntity<Object> handleMissingServletRequestParameter(
      MissingServletRequestParameterException ex, HttpHeaders headers,
      HttpStatus status, WebRequest request) {
      // MissingServletRequestParameterException handling code goes here.
  }
  ...
  @ExceptionHandler(value = { Exception.class } )
  public ResponseEntity<Object> handleOtherExceptions(final Exception ex, 
    final WebRequest req) {
    ...
  }

}

希望这能有所帮助。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23244471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档