Maven Spring Boot:运行RESTapi时出现问题



我第一次使用Spring BootMaven,并且一直在学习本教程,并对我需要做的事情进行了一些修改。当我尝试命令时

mvn spring-boot:run -e >> output.txt

构建失败了,我得到了以下输出,对我来说大约是500行胡言乱语。通读这篇文章,我不知道出了什么问题。

Pet.Java

package com.Me;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.HashSet;
import java.util.Set;
@Entity
public class Pet {
@Id
@GeneratedValue
private Long id;
public Long getId() {
    return id;
}
public String getName() {
    return name;
}
public String getPhoto() {
    return photo;
}
public String getStatus() {
    return status;
}
@JsonIgnore
public String name;
public String photo;
public String status;
public Pet(String name, String photo, String status) {
    this.name = name;
    this.photo = photo;
    this.status = status;
}
Pet() {
}
}

PetRepo.Java

package com.Me;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface PetRepo extends JpaRepository<Pet, Long> {
    Optional<Pet> findByName(String name);
    Optional<Pet> findByStatus (String status);
}

PetstoreApplicationJava

package com.Me;
import java.util.Arrays;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class PetstoreApplication {
    @Bean
    CommandLineRunner init(PetRepo petRepo) {
        return (evt) -> Arrays.asList(
                "jhoeller,dsyer,pwebb,ogierke,rwinch,mfisher,mpollack,jlong".split(","))
                .forEach(
                        a -> {
                            Pet pet = petRepo.save(new Pet(a, "meh", "Meh"));
                        });
    }
    public static void main(String[] args) {
        SpringApplication.run(PetstoreApplication.class, args);
    }
}
@RestController
@RequestMapping("/{userId}/bookmarks")
class PetRestController {
    private final PetRepo petRepo;
    @RequestMapping(value="/{petId}", method = RequestMethod.GET)
    Pet getPet(@PathVariable Long petId) {
        return this.petRepo.findOne(petId);
    }
    @Autowired
    PetRestController(PetRepo petRepo){
        this.petRepo = petRepo;
    }
}

Pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
<groupId>com.Me</groupId>
<artifactId>petstore</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>petstore</name>
<description>Petstore Project</description>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</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-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

我真的不知道发生了什么,如果能帮忙解决这个问题,我将不胜感激。

我认为线索在第112行。您还没有提供数据库配置,所以spring-boot-starter数据jpa正在尝试自动找到一个嵌入的数据库配置,我没有看到在列出的依赖项中加载一个。您还没有提供pom.xml,这将是有用的,但如果您没有类似的东西,请尝试添加以下依赖项:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.2.132</version>
</dependency>

请参阅http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html第29.1.1.条。

BTW通常,在诊断此类问题时,您希望查找报告的第一个异常,然后查找最后一个(最接近底部)"由"异常引起的异常。

HTH

最新更新