在Spring Boot v2.2.2.RELEASE中的异常处理程序不起作用?

huangapple go评论98阅读模式
英文:

Exception Handler in Spring boot v 2.2.2.RELEASE not working?

问题

以下是您提供的代码部分的中文翻译:

FileRestService 类:

  1. package com.file.web;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import javax.annotation.processing.FilerException;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.http.HttpStatus;
  8. import org.springframework.http.ResponseEntity;
  9. import org.springframework.web.bind.annotation.CrossOrigin;
  10. import org.springframework.web.bind.annotation.PostMapping;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RequestMethod;
  13. import org.springframework.web.bind.annotation.RequestParam;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import org.springframework.web.multipart.MultipartFile;
  16. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  17. import com.file.entities.FileRepository;
  18. import com.file.service.FileService;
  19. @CrossOrigin("*")
  20. @RestController
  21. public class FileRestService {
  22. @Autowired
  23. FileRepository fileRepository;
  24. @Autowired
  25. FileService fileService;
  26. @RequestMapping(value="/upload/file", method=RequestMethod.POST)
  27. public ResponseEntity<Object> uploadFile(@RequestParam("file") MultipartFile file) throws FilerException {
  28. fileService.uploadFile(file);
  29. return new ResponseEntity<Object>("文件上传成功", HttpStatus.OK);
  30. }
  31. }

AppExceptionHandeller 类:

  1. package com.file.web.exeception;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.http.HttpHeaders;
  5. import org.springframework.http.HttpStatus;
  6. import org.springframework.http.ResponseEntity;
  7. import org.springframework.web.bind.annotation.ControllerAdvice;
  8. import org.springframework.web.bind.annotation.ExceptionHandler;
  9. import org.springframework.web.context.request.WebRequest;
  10. @ControllerAdvice
  11. public class AppExceptionHandeller {
  12. @ExceptionHandler(FileException.class)
  13. public ResponseEntity<Object> handleException(FileStorageException ex, WebRequest request) {
  14. List<String> errors = new ArrayList<String>();
  15. errors.add("会话已过期");
  16. ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errors);
  17. return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
  18. }
  19. }

FileServiceImp 类:

  1. package com.file.service;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Path;
  6. import java.nio.file.Paths;
  7. import java.nio.file.StandardCopyOption;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.util.StringUtils;
  10. import org.springframework.web.multipart.MultipartFile;
  11. import com.file.web.exeception.FileStorageException;
  12. @Service
  13. public class FileServiceImp implements FileService {
  14. public String uploadDir="/Users/mac/Desktop/upload";
  15. @Override
  16. public void uploadFile(MultipartFile file) {
  17. Path copyLocation = Paths.get(uploadDir + File.separator + StringUtils.cleanPath(file.getOriginalFilename()));
  18. try {
  19. Files.copy(file.getInputStream(), copyLocation, StandardCopyOption.REPLACE_EXISTING);
  20. } catch (IOException e) {
  21. System.out.println("空文件...");
  22. throw new FileStorageException("无法存储文件 " + file.getOriginalFilename() + "。请重试!");
  23. }
  24. }
  25. @Override
  26. public void DownloadFile() {
  27. }
  28. }

FileStorageException 类:

  1. package com.file.web.exeception;
  2. public class FileStorageException extends RuntimeException {
  3. private static final long serialVersionUID = 1L;
  4. private String msg;
  5. public FileStorageException(String msg) {
  6. this.msg = msg;
  7. }
  8. public String getMsg() {
  9. return msg;
  10. }
  11. }

请注意,我只提供了代码部分的翻译,不包括代码外的附加内容。如果您有任何其他问题,欢迎继续询问。

英文:

I am implementing global error handling in my project using Spring boot. I am throwing a "FileStorageException " when my "File" object is "null". But the Spring exception handler is not catching it.

FileRestService class:

  1. package com.file.web;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import javax.annotation.processing.FilerException;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.http.HttpStatus;
  8. import org.springframework.http.ResponseEntity;
  9. import org.springframework.web.bind.annotation.CrossOrigin;
  10. import org.springframework.web.bind.annotation.PostMapping;
  11. import org.springframework.web.bind.annotation.RequestBody;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RequestMethod;
  14. import org.springframework.web.bind.annotation.RequestParam;
  15. import org.springframework.web.bind.annotation.RestController;
  16. import org.springframework.web.multipart.MultipartFile;
  17. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  18. import com.file.entities.FileRepository;
  19. import com.file.service.FileService;
  20. @CrossOrigin(&quot;*&quot;)
  21. @RestController
  22. public class FileRestService {
  23. @Autowired
  24. FileRepository fileRepository;
  25. @Autowired
  26. FileService fileService;
  27. @RequestMapping(value=&quot;/upload/file&quot;,method=RequestMethod.POST)
  28. public ResponseEntity&lt;Object&gt; uploadFile(@RequestParam(&quot;file&quot;) MultipartFile file) throws FilerException {
  29. fileService.uploadFile(file);
  30. return new ResponseEntity&lt;Object&gt;(&quot;file is uploaded successfuly&quot;,HttpStatus.OK);
  31. }
  32. }

AppExceptionHandeller class

  1. package com.file.web.exeception;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.http.HttpHeaders;
  5. import org.springframework.http.HttpStatus;
  6. import org.springframework.http.ResponseEntity;
  7. import org.springframework.web.bind.annotation.ControllerAdvice;
  8. import org.springframework.web.bind.annotation.ExceptionHandler;
  9. import org.springframework.web.context.request.WebRequest;
  10. @ControllerAdvice
  11. public class AppExceptionHandeller {
  12. @ExceptionHandler(FileException.class)
  13. public ResponseEntity&lt;Object&gt; etExcetion(FileStorageException ex, WebRequest request) {
  14. List&lt;String&gt; errors = new ArrayList&lt;String&gt;();
  15. errors.add(&quot;session exipred&quot;);
  16. ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errors);
  17. return new ResponseEntity&lt;Object&gt;(apiError, new HttpHeaders(), apiError.getStatus());
  18. }
  19. }

FileServiceImp class

  1. package com.file.service;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Path;
  6. import java.nio.file.Paths;
  7. import java.nio.file.StandardCopyOption;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.util.StringUtils;
  10. import org.springframework.web.multipart.MultipartFile;
  11. import com.file.web.exeception.FileStorageException;
  12. @Service
  13. public class FileServiceImp implements FileService {
  14. public String uploadDir=&quot;/Users/mac/Desktop/upload&quot;;
  15. @Override
  16. public void uploadFile(MultipartFile file) {
  17. //StringUtils.cleanPath(file.getOriginalFil);
  18. Path copyLocation=Paths.get(uploadDir + File.separator + StringUtils.cleanPath(file.getOriginalFilename())); ;
  19. try {
  20. Files.copy(file.getInputStream(), copyLocation, StandardCopyOption.REPLACE_EXISTING);
  21. } catch (IOException e) {
  22. // TODO Auto-generated catch block
  23. System.out.println(&quot;empty file ...&quot;);
  24. throw new FileStorageException(&quot;Could not store file &quot; + file.getOriginalFilename()
  25. + &quot;. Please try again!&quot;);
  26. //e.printStackTrace();
  27. }
  28. }
  29. @Override
  30. public void DownloadFile() {
  31. }
  32. }

FileStorageException class

  1. package com.file.web.exeception;
  2. public class FileStorageException extends RuntimeException {
  3. private static final long serialVersionUID = 1L;
  4. private String msg;
  5. public FileStorageException(String msg) {
  6. this.msg = msg;
  7. }
  8. public String getMsg() {
  9. return msg;
  10. }
  11. }

observe the FileRestService class. When I get a request for a file , if the file is not present it throws
exception. By default this needs to be catched by the ExceptionHandler. But instead of this I am getting the stackTrace like following. Please help me in catching this exception globally.

I followed this tuto (https://devwithus.com/exception-handling-for-rest-api-with-spring-boot/)
and this (https://stackoverflow.com/questions/48501265/exception-handler-in-spring-boot-not-working)
I can’t find the solution I’m starting in spring

  1. javax.annotation.processing.FilerException: Could not store file . Please try again!
  2. at com.file.service.FileServiceImp.uploadFile(FileServiceImp.java:34) ~[classes/:na]
  3. at com.file.web.FileRestService.uploadFile(FileRestService.java:41) ~[classes/:na]
  4. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
  5. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
  6. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
  7. at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
  8. at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  9. at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  10. at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  11. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  12. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  13. at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  14. at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  15. at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  16. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  17. at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  18. at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
  19. at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  20. at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
  21. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
  22. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
  23. at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.29.jar:9.0.29]
  24. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
  25. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
  26. at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  27. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  28. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
  29. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
  30. at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  31. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  32. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
  33. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
  34. at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  35. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
  36. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
  37. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
  38. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.29.jar:9.0.29]
  39. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.29.jar:9.0.29]
  40. at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526) [tomcat-embed-core-9.0.29.jar:9.0.29]
  41. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.29.jar:9.0.29]
  42. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.29.jar:9.0.29]
  43. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.29.jar:9.0.29]
  44. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.29.jar:9.0.29]
  45. at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367) [tomcat-embed-core-9.0.29.jar:9.0.29]
  46. at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.29.jar:9.0.29]
  47. at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860) [tomcat-embed-core-9.0.29.jar:9.0.29]
  48. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1591) [tomcat-embed-core-9.0.29.jar:9.0.29]
  49. at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.29.jar:9.0.29]
  50. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_212]
  51. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_212]
  52. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.29.jar:9.0.29]
  53. at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]

答案1

得分: 0

我发现解决方案只是将FileException替换为FileStorageException

在FileRestService(将FilerException替换为FileStorageException)

在AppExceptionHandeller中(@ExceptionHandler(FileException.class)替换为@ExceptionHandler(FileStorageException.class))

英文:

I find solution just replaces FileException by FileStorageException

in FileRestService(throws FilerException... by throws FileStorageException)

in AppExceptionHandeller (@ExceptionHandler(FileException.class) by @ExceptionHandler(FileStorageException.class))

huangapple
  • 本文由 发表于 2020年10月27日 04:58:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64544884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定