问题
I am using correct NEO4J_URL
environment variable as shown in heroku config
.
If I use the same neo4j url via a browser, it works fine (no auth failure). However my application fails to start with below error on heroku. (I am using spring-data-neo4j)
The URL looks like http://username:password@123456ac6.hosted.neo4j.org:1234/db/data/
. Tried without '/db/data' at the end and also with and without trailing slash. Didn't help.
Please help if anyone has already faced/resolved this issue. The stack trace on server startup:
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m Caused by:
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m at org.neo4j.rest.graphdb.index.RestIndexManager.existsForNodes(RestIndexManager.java:45)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m java.lang.RuntimeException: Error reading as JSON '<html>|<head>|<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>|<tit
le>Error 401 UNAUTHORIZED</title>|</head>|<body><h2>HTTP ERROR 401</h2>|<p>Problem accessing /db/data/index/node. Reason:|<pre> UNAUTHORIZED</pre></p><hr /><i><small>Powered by Jetty://</
small></i><br/> |<br/> |<br/> |<br/>
|<br/> |<br/> |<br/>
|<br/> |<br/> |<br/> |<br/>
|<br/> |<br/>
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m at org.neo4j.rest.graphdb.util.JsonHelper.jsonToSingleValue(JsonHelper.java:62)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m at org.neo4j.rest.graphdb.RequestResult.toEntity(RequestResult.java:114)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m |<br/> |<br/> |<br/>
|<br/> |<br/> |<br/>
|<br/> ||</body>|</html>|'
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m at org.neo4j.rest.graphdb.RequestResult.toMap(RequestResult.java:120)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m at org.neo4j.rest.graphdb.util.JsonHelper.readJson(JsonHelper.java:57)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m at org.neo4j.rest.graphdb.index.RestIndexManager.indexInfo(RestIndexManager.java:50)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m at org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategyFactory.chooseStrategy(TypeRepresentationStrategyFactor
y.java:56)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m at org.neo4j.rest.graphdb.ExecutingRestAPI.indexInfo(ExecutingRestAPI.java:327)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m at org.springframework.data.neo4j.config.Neo4jConfiguration$$EnhancerByCGLIB$$36bc2a37.CGLIB$typeRepresentationStrategyFactory$7(<generated>)
←[36m2013-03-06T23:44:31+00:00 app[web.1]:←[0m at org.springframework.data.neo4j.rest.SpringRestGraphDatabase.getIndex(SpringRestGraphDatabase.java:100
回答1:
As suggested by @MichaelHunger I used other SpringRestGraphDatabase
Constructor which takes username and password as separate parameters, now the neo database is accessible.
My earlier spring context looked like this
<bean id="graphDatabaseService" class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
<constructor-arg index="0" value="${NEO4J_URL}" />
</bean>
Now I changed it to as below and everything works fine! Here I provide the username and password via two additional environment variables. Yes, there is a little manual work in terms of setting username and password environment variables on heroku(or less preferably, you can hard code the same in your application). This is in addition to the NEO4J_URL environment variable automatically available after adding neo4j heroku add-on.
<bean id="graphDatabaseService" class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
<constructor-arg index="0" value="${NEO4J_URL}" />
<constructor-arg index="1" value="${NEO4J_USERNAME}" />
<constructor-arg index="2" value="${NEO4J_PASSWORD}" />
</bean>
Thanks @MichaelHunger!
来源:https://stackoverflow.com/questions/15260994/error-401-unauthorized-for-neo4j-rest-url-on-heroku