如何解决从Angular 5到Java Rest Services调用的POST请求错误问题?

时间:2018-09-26 13:52:11

标签: java angular rest http-post resteasy

当在tomcat上运行的Java应用程序收到以下错误时 试图从Angular 5应用程序向Java应用程序提交POST请求 使用宁静的服务:

  

23:26:22,191错误[BaseXMLFilter]筛选器链中的异常   javax.servlet.ServletException:Servlet执行引发异常       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:326)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:198)       在com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:244)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:83)       在org.jboss.seam.web.RewriteFilter.doFilter(RewriteFilter.java:63)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:69)       在org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:69)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:73)       在org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:69)       在org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:69)       在org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)       在org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)       在org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)       在org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)       在org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:69)       在org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:69)       在org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)       在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)       在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)       在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)       在org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:637)       在org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:318)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)       在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)       在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       在java.lang.Thread.run(Thread.java:745)

     

原因:java.lang.AbstractMethodError:javax.ws.rs.core.Response.getStatusInfo()Ljavax / ws / rs / core / Response $ StatusType;       在javax.ws.rs.WebApplicationException.computeExceptionMessage(WebApplicationException.java:211)       在javax.ws.rs.WebApplicationException。(WebApplicationException.java:185)       在org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider.readFrom(AbstractJAXBProvider.java:89)       在org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:105)       在org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.read(GZIPDecodingInterceptor.java:46)       在org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:108)       在org.jboss.resteasy.core.messagebody.ReaderUtility.doRead(ReaderUtility.java:106)       在org.jboss.resteasy.core.messagebody.ReaderUtility.doRead(ReaderUtility.java:93)       在org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:120)       在org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:93)       在org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:116)       在org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:211)       在org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:176)       在org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:166)       在org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:359)       在org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:336)       在org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:103)       在org.jboss.seam.resteasy.ResteasyResourceAdapter $ 1.process(ResteasyResourceAdapter.java:145)       在org.jboss.seam.servlet.ContextualHttpServletRequest.run(ContextualHttpServletRequest.java:53)       在org.jboss.seam.resteasy.ResteasyResourceAdapter.getResource(ResteasyResourceAdapter.java:121)       在org.jboss.seam.servlet.SeamResourceServlet.service(SeamResourceServlet.java:80)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:731)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)       ...另外44个   2018年9月26日11:26:22 org.apache.catalina.core.StandardWrapperValve调用

     

严重:Servlet [Seam Resource Servlet]在路径[/ jboss-seam-jpa]上下文中的Servlet.service()引发了异常[Servlet执行引发了异常] 有根本原因   java.lang.AbstractMethodError:javax.ws.rs.core.Response.getStatusInfo()Ljavax / ws / rs / core / Response $ StatusType;       在javax.ws.rs.WebApplicationException.computeExceptionMessage(WebApplicationException.java:211)       在javax.ws.rs.WebApplicationException。(WebApplicationException.java:185)       在org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider.readFrom(AbstractJAXBProvider.java:89)       在org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:105)       在org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.read(GZIPDecodingInterceptor.java:46)       在org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:108)       在org.jboss.resteasy.core.messagebody.ReaderUtility.doRead(ReaderUtility.java:106)       在org.jboss.resteasy.core.messagebody.ReaderUtility.doRead(ReaderUtility.java:93)       在org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:120)       在org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:93)       在org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:116)       在org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:211)       在org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:176)       在org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:166)       在org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:359)       在org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:336)       在org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:103)       在org.jboss.seam.resteasy.ResteasyResourceAdapter $ 1.process(ResteasyResourceAdapter.java:145)       在org.jboss.seam.servlet.ContextualHttpServletRequest.run(ContextualHttpServletRequest.java:53)       在org.jboss.seam.resteasy.ResteasyResourceAdapter.getResource(ResteasyResourceAdapter.java:121)       在org.jboss.seam.servlet.SeamResourceServlet.service(SeamResourceServlet.java:80)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:731)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:198)       在com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:244)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:83)       在org.jboss.seam.web.RewriteFilter.doFilter(RewriteFilter.java:63)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:69)       在org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:69)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:73)       在org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:69)       在org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:69)       在org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)       在org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)       在org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)       在org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)       在org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:69)       在org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)       在org.jboss.seam.servlet.SeamFilter $ FilterChainImpl.doFilter(SeamFilter.java:69)       在org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)       在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)       在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)       在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)       在org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:637)       在org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:318)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)       在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)       在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       在java.lang.Thread.run(Thread.java:745)

Angular 5应用程序代码:

const httpOptions = {
    headers: new HttpHeaders({ 'Accept': 'application/json', 'Content-Type': 'application/json' })
};
private baseUrl = 'http://localhost:8080/jboss-seam-jpa/seam/resource/rest';

 addLocation(locationTemp: Location): Observable<any> {
    return this.http.post(`${this.baseUrl}/Trips/Location`, JSON.stringify(locationTemp), httpOptions);
}

Java代码:

@POST
@Path("Location")
@Consumes("application/json")
public Response addLocation(Location location) {
        myDao.addLocation(location);
        return Response.ok().entity("Location added successfully").build();
}

java应用程序中的完整jars库:

enter image description here

谢谢!

1 个答案:

答案 0 :(得分:-1)

从stacktrace看来,您正在从浏览器的不同主机和端口发出请求,而Tomcat不喜欢这样。我看到了两种可能的解决方案,一种是配置Tomcat CORS过滤器以接受来自所有主机和端口的请求,或者仅接受您想要的一种,尽管这可能不是您在生产中想要的解决方案。

如果您使用的是Angular CLI,则可以为您的Web应用程序(在本例中为Tomcat Application Server)配置proxy

干杯。