简体   繁体   English

我应该如何使用 SpringBoot 连接到 ArangoDB

[英]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.我正在尝试创建一个简单的 SpringBoot 应用程序来连接并在数据库中存储一些数据,但是当我的代码尝试提交一个新对象时,仍然会遇到 401 禁止消息。

How should I be connecting to ArangoDb using SpringBoot to be able to save a node in the database?我应该如何使用 SpringBoot 连接到 ArangoDb 以便能够在数据库中保存节点?

I have ArangoDb running on my system, I am able to login to web console at localhost: http://localhost:8529我的系统上运行着 ArangoDb,我可以在本地主机上登录到 Web 控制台: 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).在运行应用程序之前,我还尝试通过 Web 界面创建集合(是的,我不熟悉 DB 图形)。

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!!我知道这将是“显而易见的”,至少在我使用 ArangoDb 一段时间后会如此!!

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).该错误可能表明我的依赖项中有错误的驱动程序,但如果不是我的 Gradle 文件(如下)中的驱动程序,我不知道它应该是什么。

Some code:一些代码:

application.properties: Trying to use the root user to avoid user permission issues. application.properties:尝试使用 root 用户来避免用户权限问题。 I am able to login to the web interface with this user.我可以使用该用户登录到 Web 界面。

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

Application.java应用程序.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 PostConstruct.java :不打算保留这个,只是尝试在启动后运行一些代码

@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): bean 类...使用 lombok 注释(以前从未出现过问题),为简洁起见省略了字段(仅字符串和整数):

@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:项目gradle文件:

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.堆栈跟踪片段...看起来 401 是因为没有可供 Hikari 使用的驱动程序,但我认为我已包含在上面的 Gradle 配置中。

2020-01-30 23:27:43.932 DEBUG 14845 --- [ main] osboot.diagnostics.FailureAnalyzers : FailureAnalyzer org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer@372461a9 failed 2020-01-30 23:27:43.932 DEBUG 14845 --- [主要] osboot.diagnostics.FailureAnalyzers:FailureAnalyzer org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer@972461a 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] java.lang.TypeNotPresentException:类型 org.springframework.jdbc.CannotGetJdbcConnectionException 不存在于 java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:117) ~[na:na] 在 java.base/ sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125) ~[na:na] 在 java.base/sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49) ~[na] :na] 在 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 2020-01-30 23:27:43.935 错误 14845 --- [main] osboot.SpringApplication:应用程序运行失败

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'postConstruction': Invocation of init method failed; org.springframework.beans.factory.BeanCreationException:创建名为“postConstruction”的 bean 时出错:调用 init 方法失败; nested exception is org.springframework.dao.PermissionDeniedDataAccessException: Response: 401, Error: 401 - unauthorized;嵌套异常是 org.springframework.dao.PermissionDeniedDataAccessException:响应:401,错误:401 - 未经授权; 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]嵌套异常是 com.arangodb.ArangoDBException: Response: 401, Error: 401 - 在 org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160) 〜5ELE2.2.2.2.2.160 .jar:5.2.2.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]在 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).请从 build.gradle 中删除 compile 'com.arangodb:arangodb-java-driver',因为它将由 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.当我尝试将 arangodb 驱动程序用于 Spring Boot 应用程序时,我也遇到了这个问题。 But now I have changed to user spring boot arangodb starter, now it is started working.但是现在我已经更改为用户 spring boot arangodb starter,现在它开始工作了。

You can try to use arangodb-spring-boot-starter to get this running.您可以尝试使用 arangodb-spring-boot-starter 来运行它。

build.gradle file snippet: build.gradle 文件片段:

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我在 application.properties 中有定义数据库值的属性,例如用户名/主机等

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).在那里我有一个方法 arrango() 将硬接线值传递给 bean 创建,这些硬接线值不正确(摆弄在线找到的几个教程/说明的结果)。

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.我删除了那个配置类,一切似乎都开始了,包括我将文档插入到@PostConstruct 块中的集合中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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