【问题标题】:Maven Spring-Boot: Problems running REST apiMaven Spring-Boot:运行 REST api 的问题
【发布时间】:2016-09-16 18:24:44
【问题描述】:

我是第一次同时使用 Spring BootMaven,并且一直在关注 This tutorial,并针对我需要做的一些修改。我已经完成了“构建 REST API”部分,我想运行应用程序以确保到目前为止我所拥有的内容确实有效。当我尝试命令时

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

构建失败,我得到the following output,对我来说大约是五百行乱码。读完这篇,我不知道出了什么问题。

宠物.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

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>

我真的不确定发生了什么,希望能帮助解决这个问题。

【问题讨论】:

标签: java spring rest maven spring-mvc


【解决方案1】:

我认为线索在第 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 节。

顺便说一句,通常在诊断此类问题时,您希望查找报告的第一个异常,然后针对该异常查看最后一个(最接近底部的)“由”异常引起。

HTH

【讨论】:

  • 刚刚添加了我的 pom.xml。那里列出的任何内容都不会填补数据库配置的位置,对吗?
  • 不会像其他 spring bean 那样配置数据库配置,因此您没有任何配置。通过将上述依赖项添加到您的 pom 中,您可以在类路径中使用 H2 嵌入式数据库,并且 spring-boot-data-jpa 将自动使用它。可能还有其他的也可以,但 H2 是我使用的,我知道它会自动运行。
  • 澄清一下 - 配置数据库是 Spring 关注的问题,与 Maven 无关。如果您的数据库是外部的,那么您需要在 Spring 配置中对其进行配置。 Maven 参与其中的原因纯粹是为了使嵌入式数据库可用——它没有配置它——而是因为它已经在类路径上可用,Spring Boot “看到”它并且没有任何其他配置将自动配置嵌入式。希望这是有道理的。
  • 谢谢,我试试 h2
猜你喜欢
  • 2022-11-03
  • 2017-12-01
  • 2018-10-06
  • 2021-03-07
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多