我想在Spring Boot中实现的Heroku上创建一个Web应用程序。最后,我设法通过“ Whitelabel error page 404”部署了一个普通项目,确认了部署的正确性。
我跟着(几乎一样):https://devcenter-assets0.herokucdn.com/articles/spring-boot-memcache
不幸的是,我在Spring Data JPA中仅遇到一个(我希望)问题(添加了一项服务(例如Spring Security和Lombok正常运行)之后),这使我无法构建应用程序和使用PostgreSQL:
Description:
Parameter 0 of constructor in io.github.plkpiotr.fifabackend.controller.TaskController required a bean named 'entityManagerFactory' that could not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
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>io.github.plkpiotr</groupId>
<artifactId>fifa-backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>fifa-backend</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-security</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties:
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.maxActive=10
spring.datasource.maxIdle=5
spring.datasource.minIdle=2
spring.datasource.initialSize=5
spring.datasource.removeAbandoned=true
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Task.java:
package io.github.plkpiotr.fifabackend.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
@Entity
public class Task {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@NotNull
private String name;
public Task() {}
public Task(String name) {
this.name = name;
}
public Long getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return String.format("Task[id=%d, name='%s']", this.id, this.name);
}
}
TaskRepository.java:
package io.github.plkpiotr.fifabackend.repository;
import io.github.plkpiotr.fifabackend.model.Task;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface TaskRepository extends CrudRepository<Task, Long> {}
TaskController.java:
package io.github.plkpiotr.fifabackend.controller;
import javax.validation.Valid;
import java.lang.Iterable;
import io.github.plkpiotr.fifabackend.model.Task;
import io.github.plkpiotr.fifabackend.repository.TaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/")
public class TaskController {
@Autowired
private TaskRepository taskRepo;
@Autowired
public TaskController(TaskRepository repo) {
this.taskRepo = repo;
}
@RequestMapping(method = RequestMethod.GET)
public String showAllTasks(ModelMap model) {
Iterable<Task> tasks = this.taskRepo.findAll();
model.addAttribute("tasks", tasks);
model.addAttribute("newTask", new Task());
return "task";
}
@RequestMapping(method = RequestMethod.POST)
public String newTask(ModelMap model,
@ModelAttribute("newTask") @Valid Task task,
BindingResult result) {
if (!result.hasErrors()) {
this.taskRepo.save(task);
}
return showAllTasks(model);
}
@RequestMapping(method = RequestMethod.DELETE)
public String deleteTask(ModelMap model, @RequestParam("taskId") Long id) {
this.taskRepo.deleteById(id);
return showAllTasks(model);
}
}
我尝试过:
1)。添加差异化的依赖项(h2,hibernate-core等)
2)。删除/.m2/文件夹并重新下载依赖项
3)。在其他失败的尝试之后不要哭泣...
如果您被克隆存储库所吸引: https://github.com/plkpiotr/fifa-backend
除此之外,如果我在TaskController.java中删除,则会得到:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
如何解决问题?
答案 0 :(得分:0)
@EntityScan
,@EnableJpaRepositories
和@ComponentScan
不支持通配符语法。例如,您当前使用的是:
@EntityScan("io.github.plkpiotr.*")`
但它应该只是基本软件包名称
@EntityScan("io.github.plkpiotr")