当前位置: 首页 > 知识库问答 >
问题:

为什么我会在SpringBoot中得到错误工厂方法'hal linkdisocoverer'抛出的异常?

华懿轩
2023-03-14

嗨,我是springboot新手,我试图用它创建RESTful API,当我用它创建controller时,它已经起作用了,但当我试图在pom.xml中添加这一行时

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.3.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>
    <groupId>com.movie.db</groupId>
    <artifactId>KotakKantorMovieDB</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>KotakKantorMovieDB</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>13</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
package com.movie.db.KotakKantorMovieDB.controllers;

import com.movie.db.KotakKantorMovieDB.models.Customer;
import com.movie.db.KotakKantorMovieDB.repository.CustomerRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class CustomerController {

  @Autowired
  private CustomerRepository repository;

  @GetMapping("/customer")
  public Customer findCustomer() {
    return repository.findByFirstName("cactuspot");
  }

  @PostMapping("/addcustomer")
  public Customer addCustomer(@RequestBody Customer newCustomer ) {
    return repository.save(newCustomer);
  }

}
package com.movie.db.KotakKantorMovieDB.models;

import org.springframework.data.annotation.Id;

public class Customer {
    @Id
    public String id;

    public String firstName;
    public String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}
package com.movie.db.KotakKantorMovieDB.repository;

import java.util.List;

import com.movie.db.KotakKantorMovieDB.models.Customer;

import org.springframework.data.mongodb.repository.MongoRepository;

public interface CustomerRepository extends MongoRepository<Customer, String> {

    public Customer findByFirstName(String firstName);
    public List<Customer> findByLastName(String lastName);

}

我错过了什么?如果我添加spring-boot-starter-data-rest,是否有一些不应该使用的依赖项?

共有1个答案

谭彦
2023-03-14

我试图删除:

<dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
</dependency>

这个依赖项,它解决了错误,我不知道为什么这个依赖项会导致错误,我也不知道为什么需要这个依赖项,我运行我的应用程序,没有这个依赖项,它就能正常工作。也许和Hateoas有关。如果有人能向我解释,我会接受它作为答案

 类似资料: