Page 1 of 1

Exposing the REST Apis

Posted: Tue Feb 23, 2016 6:47 am
by supraja
Im getting the below error when I run the server after implementing the steps given under REST tutorials:

Can somebody please help?


[artifact:mvn] [ERROR] 16:59:34 ContextLoader - Context initialization failed
[artifact:mvn] org.springframework.context.ApplicationContextException: I/O error parsing bean definition source for Root WebApplicationContext; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext-rest-api.xml]
[artifact:mvn] at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:135)
[artifact:mvn] at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
[artifact:mvn] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)
[artifact:mvn] at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
[artifact:mvn] at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
[artifact:mvn] at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
[artifact:mvn] at org.broadleafcommerce.common.web.extensibility.MergeContextLoader.contextInitialized(MergeContextLoader.java:148)
[artifact:mvn] at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
[artifact:mvn] at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
[artifact:mvn] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
[artifact:mvn] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
[artifact:mvn] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
[artifact:mvn] at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[artifact:mvn] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[artifact:mvn] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[artifact:mvn] at java.lang.Thread.run(Thread.java:745)
[artifact:mvn] Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext-rest-api.xml]
[artifact:mvn] at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)
[artifact:mvn] at org.broadleafcommerce.common.web.extensibility.MergeXmlWebApplicationContext.loadBeanDefinitions(MergeXmlWebApplicationContext.java:120)
[artifact:mvn] at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
[artifact:mvn] at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
[artifact:mvn] ... 15 more
[artifact:mvn] Feb 23, 2016 4:59:34 PM org.apache.catalina.core.StandardContext listenerStart
[artifact:mvn] SEVERE: Exception sending context initialized event to listener instance of class org.broadleafcommerce.common.web.extensibility.MergeContextLoader
[artifact:mvn] org.springframework.context.ApplicationContextException: I/O error parsing bean definition source for Root WebApplicationContext; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext-rest-api.xml]
[artifact:mvn] at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:135)
[artifact:mvn] at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
[artifact:mvn] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)
[artifact:mvn] at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
[artifact:mvn] at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
[artifact:mvn] at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
[artifact:mvn] at org.broadleafcommerce.common.web.extensibility.MergeContextLoader.contextInitialized(MergeContextLoader.java:148)
[artifact:mvn] at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
[artifact:mvn] at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
[artifact:mvn] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
[artifact:mvn] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
[artifact:mvn] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
[artifact:mvn] at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[artifact:mvn] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[artifact:mvn] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[artifact:mvn] at java.lang.Thread.run(Thread.java:745)
[artifact:mvn] Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext-rest-api.xml]
[artifact:mvn] at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)
[artifact:mvn] at org.broadleafcommerce.common.web.extensibility.MergeXmlWebApplicationContext.loadBeanDefinitions(MergeXmlWebApplicationContext.java:120)
[artifact:mvn] at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
[artifact:mvn] at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
[artifact:mvn] ... 15 more
[artifact:mvn] Feb 23, 2016 4:59:34 PM org.apache.catalina.core.StandardContext listenerStart
[artifact:mvn] SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
[artifact:mvn] java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!
[artifact:mvn] at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:277)
[artifact:mvn] at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
[artifact:mvn] at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
[artifact:mvn] at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
[artifact:mvn] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
[artifact:mvn] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
[artifact:mvn] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
[artifact:mvn] at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[artifact:mvn] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[artifact:mvn] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[artifact:mvn] at java.lang.Thread.run(Thread.java:745)
[artifact:mvn] Feb 23, 2016 4:59:34 PM org.apache.catalina.core.StandardContext startInternal
[artifact:mvn] SEVERE: Error listenerStart
[artifact:mvn] Feb 23, 2016 4:59:34 PM org.apache.catalina.core.StandardContext startInternal
[artifact:mvn] SEVERE: Context [] startup failed due to previous errors
[artifact:mvn] [ WARN] 16:59:34 MergeXmlWebApplicationContext - Exception thrown from ApplicationListener handling ContextClosedEvent
[artifact:mvn] java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: Root WebApplicationContext: startup date [Tue Feb 23 16:59:34 IST 2016]; root of context hierarchy
[artifact:mvn] at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:344)
[artifact:mvn] at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:331)
[artifact:mvn] at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:869)
[artifact:mvn] at org.broadleafcommerce.common.web.extensibility.MergeXmlWebApplicationContext.doClose(MergeXmlWebApplicationContext.java:178)
[artifact:mvn] at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:836)
[artifact:mvn] at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:579)
[artifact:mvn] at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:115)
[artifact:mvn] at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4980)
[artifact:mvn] at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5626)
[artifact:mvn] at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
[artifact:mvn] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)
[artifact:mvn] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
[artifact:mvn] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
[artifact:mvn] at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[artifact:mvn] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[artifact:mvn] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[artifact:mvn] at java.lang.Thread.run(Thread.java:745)
[artifact:mvn] [ WARN] 16:59:34 MergeXmlWebApplicationContext - Exception thrown from LifecycleProcessor on context close
[artifact:mvn] java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: Root WebApplicationContext: startup date [Tue Feb 23 16:59:34 IST 2016]; root of context hierarchy
[artifact:mvn] at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:357)
[artifact:mvn] at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:877)
[artifact:mvn] at org.broadleafcommerce.common.web.extensibility.MergeXmlWebApplicationContext.doClose(MergeXmlWebApplicationContext.java:178)
[artifact:mvn] at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:836)
[artifact:mvn] at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:579)
[artifact:mvn] at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:115)
[artifact:mvn] at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4980)
[artifact:mvn] at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5626)
[artifact:mvn] at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
[artifact:mvn] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)
[artifact:mvn] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
[artifact:mvn] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
[artifact:mvn] at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[artifact:mvn] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[artifact:mvn] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[artifact:mvn] at java.lang.Thread.run(Thread.java:745)
[artifact:mvn] Feb 23, 2016 4:59:34 PM org.apache.catalina.core.ApplicationContext log
[artifact:mvn] INFO: Closing Spring root WebApplicationContext
[artifact:mvn] Feb 23, 2016 4:59:34 PM org.apache.catalina.core.ApplicationContext log
[artifact:mvn] INFO: Closing Spring root WebApplicationContext
[artifact:mvn] Feb 23, 2016 4:59:34 PM org.apache.catalina.core.StandardContext listenerStop
[artifact:mvn] SEVERE: Exception sending context destroyed event to listener instance of class org.broadleafcommerce.common.web.extensibility.MergeContextLoader
[artifact:mvn] java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
[artifact:mvn] at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:170)
[artifact:mvn] at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:908)
[artifact:mvn] at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:884)
[artifact:mvn] at org.broadleafcommerce.common.web.extensibility.MergeXmlWebApplicationContext.doClose(MergeXmlWebApplicationContext.java:178)
[artifact:mvn] at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:836)
[artifact:mvn] at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:579)
[artifact:mvn] at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:115)
[artifact:mvn] at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4980)
[artifact:mvn] at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5626)
[artifact:mvn] at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
[artifact:mvn] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)
[artifact:mvn] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
[artifact:mvn] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
[artifact:mvn] at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[artifact:mvn] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[artifact:mvn] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[artifact:mvn] at java.lang.Thread.run(Thread.java:745)
[artifact:mvn] Feb 23, 2016 4:59:34 PM org.apache.catalina.core.ApplicationContext log
[artifact:mvn] INFO: Shutting down log4j
[artifact:mvn] Feb 23, 2016 4:59:34 PM org.apache.coyote.AbstractProtocol start
[artifact:mvn] INFO: Starting ProtocolHandler ["http-bio-8080"]
[artifact:mvn] Feb 23, 2016 4:59:34 PM org.apache.coyote.AbstractProtocol start
[artifact:mvn] INFO: Starting ProtocolHandler ["http-bio-8443"]



my web.xml
---------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<display-name>mycompany</display-name>

<context-param>
<param-name>webAppRootKey</param-name>
<param-value>site.root</param-value>
</context-param>

<context-param>
<param-name>patchConfigLocation</param-name>
<param-value>
classpath:/bl-open-admin-contentClient-applicationContext.xml
classpath:/bl-cms-contentClient-applicationContext.xml
classpath*:/blc-config/site/bl-*-applicationContext.xml
classpath:/applicationContext.xml
classpath:/applicationContext-email.xml
/WEB-INF/applicationContext-datasource.xml
/WEB-INF/applicationContext-rest-api-security.xml
/WEB-INF/applicationContext-rest-api.xml
/WEB-INF/applicationContext-security.xml
/WEB-INF/applicationContext-filter.xml
/WEB-INF/applicationContext-workflow.xml
/WEB-INF/applicationContext.xml
</param-value>
</context-param>

<context-param>
<param-name>shutdownHookMethod</param-name>
<param-value>forceFlush</param-value>
</context-param>

<absolute-ordering />

<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- enable configured logging -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<listener>
<listener-class>org.broadleafcommerce.common.web.extensibility.MergeContextLoader</listener-class>
</listener>

<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Note: The applicationContext-servlet-cms-contentClient.xml file is used to load static assets
from the Broadleaf CMS such as images. It will match on any URL that contains the static asset
prefix URL in any depth of it. -->
<servlet>
<servlet-name>mycompany</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/blc-config/site/bl-*-applicationContext-servlet.xml
classpath:/applicationContext-servlet-cms-contentClient.xml
/WEB-INF/applicationContext-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mycompany</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


<!-- The RESTApiServlet can be disabled, but doesn't have to be. It will throw an exception if
it is accessed when no endpoints have been registered via Spring. Add /WEB-INF/applicationContext-rest-api.xml
to the merge patchConfigLocation ABOVE applicationContext-security.xml to activate and begin using REST services.
Alternately, this servlet and servlet mapping can be commented out entirely. -->
<servlet>
<servlet-name>RESTApiServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
com.mycompany.api.RestApiMvcConfiguration
</param-value>

</init-param>
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>RESTApiServlet</servlet-name>
<url-pattern>/api/v1/*</url-pattern>
</servlet-mapping>


<env-entry>
<env-entry-name>appName</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>mycompanySite</env-entry-value>
</env-entry>

</web-app>

Re: Exposing the REST Apis

Posted: Thu Mar 10, 2016 11:24 pm
by chandrabhanurastogi
Hi,

A am getting the same issue. Can you explain How did you resolved it?

Tutorial explains to add JerseyServlet but in site's web.xml RESTApiServlet is already added so I did not included JerseyServlet.

Re: Exposing the REST Apis

Posted: Thu Mar 10, 2016 11:26 pm
by chandrabhanurastogi
Hi,

I am getting the same issue. can you explain how you resolved it?

Re: Exposing the REST Apis

Posted: Fri Mar 18, 2016 3:33 pm
by homr_zodyssey
It looks like /WEB-INF/applicationContext-rest-api.xml was changed to /WEB-INF/applicationContext-rest-api-security.xml over a year ago. It's already referenced in web.xml. I don't think you have to do anything to enable the REST API anymore.

However, it's not working for me. The endpoints are there and listening, but they respond with:

Code: Select all

{
    "httpStatusCode":415,
    "message":[
        {
            "messageKey":"contentTypeNotSupported",
            "message":"The Content-Type header null is not supported. Please switch the Content-Type of your request to one of the following: application/json or application/xml"
        }
    ]
}


This error message is probably incorrect, because something called "Jackson" has a known bug that generates it when there's a problem with serialization. http://forum.spring.io/forum/spring-projects/web/77132-json-httpmediatypenotsupportedexception

I'm not sure what to do about that.