hi all,

I'm creating a rest API for one application. It's working now. I would like to handle some specific exceptions and to return some specific data responses. I used @Provider from javax.ws.rs.ext.Provider to handle it, but I got an error 500.

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import ch.axonivy.fintech.export.dossier.exception.RecordNotFoundException;
import ch.axonivy.fintech.export.dossier.response.ExportingErrorResponse;

@Provider
public class RecordNotFoundExceptionMapper implements ExceptionMapper<RecordNotFoundException> {

    @Override
    public Response toResponse(RecordNotFoundException ex) {
        ExportingErrorResponse response = new ExportingErrorResponse();
        response.setStatusCode(Status.NOT_FOUND.getStatusCode());
        response.setErrorMessage(ex.getMessage());

        return Response.status(response.getStatusCode()).entity(response).type(MediaType.APPLICATION_JSON).build();
    }
}

The following is error:

14:59:41.299 WARN [ch.ivyteam.bus.internal] [Worker-45] [executionContext=SYSTEM] 
  Error occured: 
    java.lang.NullPointerException
        at org.glassfish.jersey.server.ResourceBag$Builder.registerModel(ResourceBag.java:116)
        at org.glassfish.jersey.server.ResourceBag$Builder.registerProgrammaticResource(ResourceBag.java:110)
        at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:459)
        at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:184)
        at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350)
        at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255)
        at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:347)
        at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392)
        at org.glassfish.jersey.servlet.ServletContainer.reload(ServletContainer.java:681)
        at ch.ivyteam.ivy.webserver.internal.rest.RestLifecycleListener.reloadInContext(RestLifecycleListener.java:62)
        at ch.ivyteam.ivy.webserver.internal.rest.RestLifecycleListener.reload(RestLifecycleListener.java:47)
        at ch.ivyteam.ivy.webserver.internal.rest.RestLifecycleListener.handleDeprectatedClassLoader(RestLifecycleListener.java:40)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at net.engio.mbassy.dispatch.ReflectiveHandlerInvocation.invoke(ReflectiveHandlerInvocation.java:29)
        at net.engio.mbassy.dispatch.MessageDispatcher.dispatch(MessageDispatcher.java:30)
        at net.engio.mbassy.dispatch.FilteredMessageDispatcher.dispatch(FilteredMessageDispatcher.java:42)
        at net.engio.mbassy.subscription.Subscription.publish(Subscription.java:72)
        at net.engio.mbassy.bus.MessagePublication.execute(MessagePublication.java:49)
        at net.engio.mbassy.bus.MBassador.publish(MBassador.java:66)
        at ch.ivyteam.bus.internal.EventBus.publish(EventBus.java:30)
        at ch.ivyteam.ivy.java.internal.ClassLoaderHolder.deprecateClassLoaderInternal(ClassLoaderHolder.java:349)
        at ch.ivyteam.ivy.java.internal.ClassLoaderHolder.deprecateClassLoader(ClassLoaderHolder.java:323)
        at ch.ivyteam.ivy.java.internal.ClassLoaderHolder.reinstantiateClassLoader(ClassLoaderHolder.java:299)
        at ch.ivyteam.ivy.java.internal.ClassloaderReinstatiateCompilationParticipant.reinstatiateProjectClassloader(ClassloaderReinstatiateCompilationParticipant.java:82)
        at ch.ivyteam.ivy.java.internal.ClassloaderReinstatiateCompilationParticipant.buildFinished(ClassloaderReinstatiateCompilationParticipant.java:70)
        at org.eclipse.jdt.internal.core.builder.JavaBuilder.build(JavaBuilder.java:235)
        at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:734)
        at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
        at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:206)
        at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:246)
        at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:299)
        at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
        at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:302)
        at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:358)
        at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:381)
        at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:143)
        at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:241)
        at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
15:41:38.174 ERROR [org.apache.catalina.core.ContainerBase.[ivy].[localhost].[/ivy].[org.glassfish.jersey.servlet.ServletContainer]] [http-nio-8081-exec-1] [requestId=2832] 
  Servlet.service() for servlet [org.glassfish.jersey.servlet.ServletContainer] in context with path [/ivy] threw exception [java.lang.IllegalStateException: Request scope has been already shut down.] with root cause
    java.lang.IllegalStateException: Request scope has been already shut down.
        at jersey.repackaged.com.google.common.base.Preconditions.checkState(Preconditions.java:173)
        at org.glassfish.jersey.process.internal.RequestScope.retrieveCurrent(RequestScope.java:239)
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:314)
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
        at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
        at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at ch.ivyteam.ivy.webserver.internal.IvyFilter.doFilterInternal(IvyFilter.java:247)
        at ch.ivyteam.ivy.webserver.internal.IvyFilter.doFilter(IvyFilter.java:152)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at ch.ivyteam.ivy.webserver.internal.IvyExecuteAsSystemFilter$1.call(IvyExecuteAsSystemFilter.java:45)
        at ch.ivyteam.ivy.webserver.internal.IvyExecuteAsSystemFilter$1.call(IvyExecuteAsSystemFilter.java:1)
        at ch.ivyteam.ivy.security.internal.SecurityManager.executeAsSystem(SecurityManager.java:1310)
        at ch.ivyteam.ivy.webserver.internal.IvyExecuteAsSystemFilter.doFilter(IvyExecuteAsSystemFilter.java:39)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.filters.HttpHeaderSecurityFilter.doFilter(HttpHeaderSecurityFilter.java:124)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at ch.ivyteam.ivy.webserver.internal.startup.IvyEngineUnavailableFilter.doFilter(IvyEngineUnavailableFilter.java:48)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at ch.ivyteam.ivy.webserver.internal.filter.IvyCurrentHttpRequestFilter.doFilter(IvyCurrentHttpRequestFilter.java:39)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:120)
        at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:135)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
        at ch.ivyteam.ivy.webserver.internal.PerformanceLogValve.invoke(PerformanceLogValve.java:55)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1457)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)
15:41:38.325 ERROR [org.apache.myfaces.application.ApplicationImpl] [http-nio-8081-exec-1] [requestId=2832] 
  Could not instantiate component componentType = guiEventListenerComponent
    [errorId=170D30E5B15459D4, requestId=2832]
    ch.ivyteam.ivy.environment.EnvironmentNotAvailableException: Access to ivy environment outside a process request thread is not possible.
    Current thread: Thread[http-nio-8081-exec-1,5,main]
        at ch.ivyteam.ivy.environment.Ivy.getIvy(Ivy.java:577)
        at ch.ivyteam.ivy.environment.Ivy.getEnvironmentData(Ivy.java:556)
        at ch.ivyteam.ivy.environment.Ivy.getEnvironmentData(Ivy.java:551)
        at ch.ivyteam.ivy.environment.Ivy.request(Ivy.java:244)
        at ch.axonivy.fintech.guiframework.component.GuiEventListenerFactoryServiceProvider.getProjectClassLoader(GuiEventListenerFactoryServiceProvider.java:67)
        at ch.axonivy.fintech.guiframework.component.GuiEventListenerFactoryServiceProvider.getFactory(GuiEventListenerFactoryServiceProvider.java:32)
        at ch.axonivy.fintech.guiframework.component.GuiEventListenerComponent.<init>(GuiEventListenerComponent.java:46)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at java.lang.Class.newInstance(Class.java:442)
        at org.apache.myfaces.application.ApplicationImpl.createComponent(ApplicationImpl.java:1469)
        at javax.faces.application.ApplicationWrapper.createComponent(ApplicationWrapper.java:129)
        at org.apache.myfaces.view.facelets.compiler.FaceletsCompilerSupport.loadLibraries(FaceletsCompilerSupport.java:141)
        at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.loadLibraries(FaceletViewDeclarationLanguage.java:2526)
        at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.createCompiler(FaceletViewDeclarationLanguage.java:2172)
        at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.initialize(FaceletViewDeclarationLanguage.java:2464)
        at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.<init>(FaceletViewDeclarationLanguage.java:309)
        at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguageStrategy.<init>(FaceletViewDeclarationLanguageStrategy.java:52)
        at org.apache.myfaces.view.ViewDeclarationLanguageFactoryImpl.initialize(ViewDeclarationLanguageFactoryImpl.java:104)
        at org.apache.myfaces.view.ViewDeclarationLanguageFactoryImpl.getViewDeclarationLanguage(ViewDeclarationLanguageFactoryImpl.java:73)
        at org.apache.myfaces.application.ViewHandlerImpl.getViewDeclarationLanguage(ViewHandlerImpl.java:183)
        at javax.faces.application.ViewHandlerWrapper.getViewDeclarationLanguage(ViewHandlerWrapper.java:133)
        at org.apache.myfaces.shared.application.DefaultViewHandlerSupport.checkResourceExists(DefaultViewHandlerSupport.java:569)
        at org.apache.myfaces.shared.application.DefaultViewHandlerSupport.handleSuffixMapping(DefaultViewHandlerSupport.java:507)
        at org.apache.myfaces.shared.application.DefaultViewHandlerSupport.calculateViewId(DefaultViewHandlerSupport.java:113)
        at org.apache.myfaces.application.ViewHandlerImpl.deriveLogicalViewId(ViewHandlerImpl.java:122)
        at javax.faces.application.ViewHandlerWrapper.deriveLogicalViewId(ViewHandlerWrapper.java:112)
        at org.apache.myfaces.lifecycle.RestoreViewExecutor.execute(RestoreViewExecutor.java:225)
        at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:196)
        at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:143)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:728)
        at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:469)
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:392)
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:311)
        at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:395)
        at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:254)
        at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:349)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:175)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
        at ch.ivyteam.ivy.webserver.internal.PerformanceLogValve.invoke(PerformanceLogValve.java:55)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1457)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)

P/s: I’m using Axon Ivy 7.0.11

Could you please give me some suggestions or any hints to overcome this issue? Thank you!

asked 13.03.2020 at 06:10

ptgiang's gravatar image

ptgiang
(suspended)
accept rate: 0%

edited 13.03.2020 at 06:11


Hi

We added support for @Provider for Ivy projects with version 7.0.13.

If you can't upgrade I would say your only other option is to add a try-catch to each API and handle the conversion there, probably in a general ExceptionHandler class. The try-catch could be handled in a Lambda expression:

@GET
@Path("/{entryNo}")
@Produces(MediaType.APPLICATION_JSON)
public Response getPerson(@PathParam("entryNo") int entryNo) {
  return ErrorHandler.withExceptionHandling( () -> {
      UUID personId = new ArrayList<>(persons.keySet()).get(entryNo);
      return Response.status(Status.OK)
              .entity(persons.get(personId))
              .build();
  });
}

And with ErrorHandler:

public class ErrorHandler {
  public static Response withExceptionHandling(Callable<Response> restCall) {
      try {
        return restCall.call();
      }
      catch (RecordNotFoundException ex)
      {
        ExportingErrorResponse response = new ExportingErrorResponse(Status.NOT_FOUND, ex.getMessage());
        return Response.status(response.getStatusCode()).entity(response).type(MediaType.APPLICATION_JSON).build();
      } catch (Exception e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }    
  }
}

Or something like that.

link

answered 16.03.2020 at 09:28

Andreas%20Rusch's gravatar image

Andreas Rusch
441145
accept rate: 92%

Thanks for your suggestion. Currently, I'm using this way and it's working now. Just give information if someone follows this way: we can only handle some exceptions from our logic, otherwise, we can not do, for example unauthorization exception, a method not found exception, ...

(16.03.2020 at 22:08) ptgiang ptgiang's gravatar image

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×147
×33

Asked: 13.03.2020 at 06:10

Seen: 1,639 times

Last updated: 16.03.2020 at 22:08