Java Spring Boot - Neo4j 入門

Java Spring Boot - Neo4j 入門

這次研究一下 Neo4j 

可以先從 入門的文章開始。

可以看以下這篇文章

https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/470109/

做為入門開始。

但有些雷,是會採到的,可能過了一段時間,版本更新了,導致有些舊方式已不支援。

要改用新的方式

如方法 findByReveueGT 已算無效了,因為我新安裝的Neo4j Desktop 版,就沒有 Revenue 的屬性

所以我改成

    @GetMapping("/findByReleasedGT")
    public Iterable<Movie> findByReleasedGT(@RequestParam Integer released) {
        return movieRepository.findByReleasedGreaterThan(released);
    }

Movie 改成


@lombok.Data
@NoArgsConstructor
@NodeEntity
public class Movie{
    @Id
    @GeneratedValue
    private Long id;
    private String title;
    private String tagline;
    private int released;

    public Movie (String title, String tagline, int released) {
        this.title = title;
        this.tagline = tagline;
        this.released = released;
    }
}

另外,下載安裝neo4j Desktop 版後,

直接打 http://127.0.0.1:7474/browser/ 

直接輸入 帳密 neo4j /  neo4j 是無效的,會出現

Neo.ClientError.Security.Unauthorized: The client is unauthorized due to authentication failure.

找到網路解如下

https://community.neo4j.com/t/connect-neo-clienterror-security-unauthorized-the-client-is-unauthorized-due-to-authentication-failure/11801/13

  1. Open neo4j browser from Neo4j desktop
  2. Use ":server user add" command to create new user
  3. Check the "Force password change" option
  4. Open common browser (chrome, firefox, etc) at http://localhost:7474/
  5. Login with the user you have created.
  6. Set a new password for the user

需要打開desktop 版,由 desktop 版,開啟 browser 

並輸入 :server user add

輸入你要新增的使用者,

新增完使用者與密碼後,開始瀏覽器,網址為 http://localhost:7474

這時用剛建的帳密登入後,並會要求你更改密碼,

之後,這個帳密,就能讓你的程式去 run ,否則會卡在無法授權的問題。

再來,依入門的文章去做的話,打API會發生

The old parameter syntax `{param}` is no longer supported. Please use `$param` instead

可能版本太舊了,

依下面這篇文章說明

https://blog.csdn.net/wang0112233/article/details/117995424

要將 spring boot 版本往上升級成 2.3.1.RELEASE

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

因為我使用的是Maven 與 YML 可能跟網路上現有的入門方式不同,

另外我有使用swagger 

所以我的 pom.xml 最終用到的一些項目關鍵內容是

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-neo4j</artifactId>
        </dependency>
        <!-- Neo4j -->
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j</artifactId>
            <version>3.2.9</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-embedded-driver</artifactId>
            <version>${neo4j-ogm.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-bolt-driver</artifactId>
            <version>${neo4j-ogm.version}</version>
        </dependency>
        <!-- Swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <swagger.version>1.6.0</swagger.version>
        <springfox.version>2.9.2</springfox.version>

YML

spring:

  data:
    neo4j:
      url: bolt:localhost
      username: neo4j
      password: neo4j

以上參考其它文章配合服用,應該就能 RUN 出你要的程式。