简体   繁体   English

Maven Spring-Boot:运行REST API时出现问题

[英]Maven Spring-Boot: Problems running REST api

I'm using both Spring Boot and Maven for the first time, and have been following This tutorial , with some modifications for what I need to do. 我第一次使用Spring BootMaven ,并且一直在关注本教程 ,并对我需要做的修改。 I've finished the "Building a REST API" Section, and I want to run the application to make sure what I have so far actually works. 我已经完成了“构建REST API”部分,并且我想运行该应用程序以确保到目前为止我已经实际使用了。 When I try the command 当我尝试命令

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

The build fails, and I get the following output , which for me is about five hundred lines of gibberish. 构建失败,我得到以下输出 ,对我来说,这大约是五百行胡言乱语。 Reading through this, I have no idea what's going wrong. 通过阅读,我不知道出了什么问题。

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);
}

PetstoreApplication.Java PetstoreApplication.Java

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 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>

I'm really not sure what's going on and would appreciate a hand with this issue. 我真的不确定发生了什么,希望能对此问题有所帮助。

I think the clue is at line 112. You haven't provided a database config so spring-boot-starter-data-jpa is trying to find an embedded one automatically and I don't see one being loaded in the listed dependencies. 我认为线索在第112行。您尚未提供数据库配置,因此spring-boot-starter-data-jpa试图自动查找嵌入式的,并且在列出的依赖项中看不到正在加载的嵌入式。 You haven't provided your pom.xml which would be useful but try adding the following dependency to it if you do not have something similar: 您尚未提供有用的pom.xml,但如果没有类似的内容,请尝试向其添加以下依赖项:

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

See http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html section 29.1.1. 参见http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html第29.1.1节。

BTW In general when diagnosing issues such as this you want to look for the first exception reported and then for that exception look at the last (nearest the bottom) "Caused by" exception. BTW通常,在诊断诸如此类的问题时,您要查找所报告的第一个异常,然后针对该异常查看最后一个(最底部)“ Caused by”异常。

HTH 高温超导

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

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