簡體   English   中英

Maven Spring-Boot:運行REST API時出現問題

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

我第一次使用Spring BootMaven ,並且一直在關注本教程 ,並對我需要做的修改。 我已經完成了“構建REST API”部分,並且我想運行該應用程序以確保到目前為止我已經實際使用了。 當我嘗試命令

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

構建失敗,我得到以下輸出 ,對我來說,這大約是五百行胡言亂語。 通過閱讀,我不知道出了什么問題。

寵物爪哇

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() {

}
}

寵物庫

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

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-data-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通常,在診斷諸如此類的問題時,您要查找所報告的第一個異常,然后針對該異常查看最后一個(最底部)“ Caused by”異常。

高溫超導

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM