我正在使用spring-web-5.0.7.RELEASE.jar注释@ControllerAdvice
当请求失败时,@ControllerAdvice
将自动生成带有一些消息的http 500。我的班级是这样做的:
@ControllerAdvice
public class ExceptionControllerAdvice {
protected final Log log = LogFactory
.getLog(ExceptionControllerAdvice.class);
@ExceptionHandler({ DataStoreException.class, Exception.class })
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity<ErrorResponse> exceptionHandler(Exception ex) {
log.error(ex.toString(),ex);
ErrorResponse error = new ErrorResponse();
error.setErrorCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
if (ex instanceof DataStoreException) {
error.setMessage(((DataStoreException) ex).getExceptionMessage());
} else if (ex instanceof UserContextException) {
error.setMessage(((UserContextException) ex).getMessage());
} else {
error.setMessage("Please contact your administrator");
}
return new ResponseEntity<ErrorResponse>(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
基于上面的日志记录,我看到许多此错误消息:
org.springframework.web.multipart.MultipartException: Current request is not a multipart request: org.springframework.web.multipart.MultipartException: Current request is not a multipart request
at deployment.docstore.war//org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:196)
at deployment.docstore.war//org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:112)
at deployment.docstore.war//org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:124)
at deployment.docstore.war//org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)
at deployment.docstore.war//org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:131)
at deployment.docstore.war//org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at deployment.docstore.war//org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
at deployment.docstore.war//org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
at deployment.docstore.war//org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at deployment.docstore.war//org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
--- and so on ---
如何跟踪请求,到达此状态后收到了什么?
仅供参考,可以使用典型的@RestController
设置端点,并且可以通过命令行 curl
访问
curl -k --header "$HEADER" -X POST "${BASE_URL}/scans/customers/${CUSTOMER_ID}" -F file=@/Users/me/Downloads/$FILENAME -F "filename=$FILENAME"
。上面的异常是由另一个程序(而不是curl)调用的。
@RestController
@RequestMapping("/api/v3/artifacts")
public class MyClass {
@RequestMapping(value = "scans/customers/{customerId}", method = RequestMethod.POST)
@Produces(MediaType.TEXT_PLAIN)
public String saveFile(@RequestParam("file") MultipartFile file, @PathVariable Long customerId,
@RequestParam(name = "filename") String fileName) throws
Exception {
String result = "NEWFILEID_IS_123"; //just an example
return result;
}
}