简体   繁体   中英

How should I connect to ArangoDB using SpringBoot

I'm trying to create a simple SpringBoot app to connect and store some data in the database, but keep hitting a 401 forbidden message when my code tries to commit a new object.

How should I be connecting to ArangoDb using SpringBoot to be able to save a node in the database?

I have ArangoDb running on my system, I am able to login to web console at localhost: http://localhost:8529

I have a database with the same name as the property below. I also tried creating collections through the web interface prior to running the app (yes, I'm new to the graph to DBs).

The error happens when trying to persist an entity:

competitorRepository.save(competitor);

I know this is going to be something 'obvious', at least it will be once I've used ArangoDb for a bit!!

The error may suggest I've got the wrong driver in my dependencies, but no idea what it should be if not what I have in my Gradle file (below).

Some code:

application.properties: Trying to use the root user to avoid user permission issues. I am able to login to the web interface with this user.

spring.data.arangodb.hosts=localhost:8529
spring.data.arangodb.database=grading-data
spring.data.arangodb.user=root
spring.data.arangodb.password=theRightPasswordForRoot

Application.java

@SpringBootApplication
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}
}

PostConstruct.java : don't plan to keep this, just trying to run some code after startup

@Component
@Slf4j
public class PostConstruction {

private Environment environment;
private CompetitorRepository competitorRepository;
private ApplicationContext ctx;

@Autowired
public PostConstruction(Environment environment, CompetitorRepository competitorRepository, ApplicationContext ctx) {
    this.environment = environment;
    this.competitorRepository = competitorRepository;
    this.ctx = ctx;
}

@PostConstruct
public void stuffToDoOnceApplicationStartsUp() {

    var competitor = Competitor.builder()
            .clubName("a club")
            .firstName("name")
            .lastName("lastname")
            .build();
    var savedCompetitor = competitorRepository.save(competitor);
    System.out.println(savedCompetitor);
}
}

The bean class... uses lombok annotations (never been a problem before), fields ommitted for brevity (just strings and ints):

@SuperBuilder
@Getter
@ToString
@Document("competitor")
@HashIndex(fields = { "licence" }, unique = true)
public class Competitor extends Person {

@Id
private String id;

}

Repository:

@Repository
public interface CompetitorRepository extends ArangoRepository<Competitor, String> {
}

project gradle file:

buildscript {
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.0.RELEASE")
}
}

plugins {
id 'java'
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.7.RELEASE'
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
manifest {
    attributes 'Start-Class': 'my.package.conf.Application'
}
launchScript()
}

repositories {
mavenCentral()
}

sourceCompatibility = 11
targetCompatibility = 11


dependencies {
compile "org.springframework.boot:spring-boot-starter-websocket"
compile "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.springframework.boot:spring-boot-starter-test" // todo : test scope
compile 'org.springframework.boot:spring-boot-starter-actuator' //     /actuator/health

compile 'org.apache.httpcomponents:httpclient:4.5.1' // for arangodb ?

compile 'com.arangodb:arangodb-spring-boot-starter:1.0.2'
compile 'com.arangodb:arangodb-spring-data:3.2.3'
compile 'com.arangodb:arangodb-java-driver'


compile 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'org.projectlombok:lombok:1.18.10'
}

Snippets of stack trace... Looks like the 401 is because no driver available for Hikari to use, but I thought I had included in Gradle config above.

2020-01-30 23:27:43.932 DEBUG 14845 --- [ main] osboot.diagnostics.FailureAnalyzers : FailureAnalyzer org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer@372461a9 failed

java.lang.TypeNotPresentException: Type org.springframework.jdbc.CannotGetJdbcConnectionException not present at java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:117) ~[na:na] at java.base/sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125) ~[na:na] at java.base/sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49) ~[na:na] at java.base/sun.reflect.generics.visitor.Reifier.reifyTypeArguments(Reifier.java:68) ~[na:na]

2020-01-30 23:27:43.935 ERROR 14845 --- [ main] osboot.SpringApplication : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'postConstruction': Invocation of init method failed; nested exception is org.springframework.dao.PermissionDeniedDataAccessException: Response: 401, Error: 401 - unauthorized; nested exception is com.arangodb.ArangoDBException: Response: 401, Error: 401 - unauthorized at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]

Please remove compile 'com.arangodb:arangodb-java-driver' from build.gradle , as it will be resolved by spring boot arangodb starter(arangodb-spring-boot-starter).

I have also faced this when i try to use arangodb driver for the spring boot app. But now I have changed to user spring boot arangodb starter, now it is started working.

You can try to use arangodb-spring-boot-starter to get this running.

build.gradle file snippet:

plugins {
    id 'org.springframework.boot' version '2.1.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
    id 'eclipse'
}

And dependencies section:

    dependencies {
       implementation 'org.springframework.boot:spring-boot-starter-web'
       implementation 'com.arangodb:arangodb-spring-boot-starter:1.0.2' 
    }

As ever with this sort of question... user error.

I had properties in an application.properties that defined values for database such as username/host etc

I also had :

@EnableArangoRepositories(basePackages = { "uk.co.deditech" })
public class ArangoGraphDbConfig implements ArangoConfiguration

In there I had a method arrango() which passed hard wired values into bean creation, those hard wired values were incorrect (a result of fiddling with several tutorials/instructions found online).

I removed that configuration class and it all seemed to start up, including my insertion of a document into a collection in a @PostConstruct block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM